Android,如何以编程方式将取消和确定按钮添加到自定义对话框?

时间:2014-04-01 14:54:32

标签: android xml

我使用此代码创建了自定义对话框:

public void showEditTextModalWindow(View view) {

    final Dialog dialog = new Dialog(PodShipmentDetail.this);
    dialog.setTitle("Edit shipment note");
    dialog.setContentView(R.layout.alert_dialog_custom);

    dialog.show();
    Log.i(GlobalApplication.APP_LOG_NAMESPACE, "SHOW DIALOG");
}

我想问一下,如何用onClick监听器以编程方式添加OK和Cancel按钮..

这样的事情:

builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
   dialog.dismiss();
}
});

我不想在XML中添加按钮,因为我希望设计中的按钮通常适用于Android中的警报对话框(不是灰色的,不是丑陋的)..

感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

custom dialog

<强> alert_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:ignore="HardcodedText,ContentDescription" android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#8aaf9b">
    <Button android:id="@+id/btn_left" android:layout_width="160dp" android:text="OK"
        android:layout_height="35dp" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" android:background="@drawable/button_left" />
    <Button android:id="@+id/btn_right" android:layout_width="160dp"
        android:layout_height="35dp" android:layout_alignParentBottom="true" android:text="Cancel"
        android:layout_alignParentRight="true" android:background="@drawable/button_right" />
    <TextView android:id="@+id/tvTitle"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:text="Title" android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView android:id="@+id/tvMessage" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_above="@+id/btn_left"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true"
        android:layout_below="@+id/tvTitle" android:text="Message" />
</RelativeLayout>

<强> AlertDialog.java

import android.app.*;
import android.content.*;
import android.view.*;
import android.widget.*;

public class AlertDialog extends Dialog {
    private Button alertOk, alertCancel;
    private TextView tvTitle, tvMessage;
    View v = null;

    public AlertDialog(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.alert_dialog);
        v = getWindow().getDecorView();
        v.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        v.setBackgroundResource(android.R.color.transparent);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvMessage = (TextView) findViewById(R.id.tvMessage);
        alertOk = (Button) findViewById(R.id.btn_left);
        alertCancel = (Button) findViewById(R.id.btn_right);
        alertOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });
        alertCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });
    }

    @Override
    public void setTitle(CharSequence title) {
        tvTitle.setText(title);
    }

    public void setMessage(CharSequence msg) {
        tvMessage.setText(msg);
    }
}

<强> left_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:bottomLeftRadius="5dp" />
    <solid android:color="#919191"/>
</shape>

<强> left_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:bottomLeftRadius="5dp" />
    <solid android:color="#5e5e5e"/>
</shape>

<强> right_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:bottomRightRadius="5dp" />
    <solid android:color="#919191"/>
</shape>

<强> right_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:bottomRightRadius="5dp" />
    <solid android:color="#5e5e5e"/>
</shape>

<强> button_left.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/left_pressed" />
    <item android:drawable="@drawable/left_normal" />
</selector>

<强> button_right.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/right_pressed" />
    <item android:drawable="@drawable/right_normal" />
</selector>

如何显示对话框

AlertDialog alertDialog = new AlertDialog(context);
alertDialog.setTitle("Custom Dialog");
alertDialog.setMessage("Custom Dialog!!\n\n\n(Facebook.com/Awais.is.King)");
alertDialog.show();

答案 1 :(得分:0)

添加确定按钮或取消并添加此代码:

Button btnOk = (Button) dialog.findViewById(R.id.dialogButton);
btnOk.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      dialog.dismiss();
   }
});

所以这应该是这样的:

public void showEditTextModalWindow(View view) {

    final Dialog dialog = new Dialog(PodShipmentDetail.this);
    dialog.setTitle("Edit shipment note");
    dialog.setContentView(R.layout.alert_dialog_custom);

    Button btnOk = (Button) dialog.findViewById(R.id.dialogButton);
    btnOk.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          dialog.dismiss();
       }
    });

    dialog.show();
    Log.i(GlobalApplication.APP_LOG_NAMESPACE, "SHOW DIALOG");
}