无法将OnClickListener添加到对话框中的按钮

时间:2016-09-15 09:56:36

标签: java android function alertdialog

我编写了一个函数来处理Dialog的显示,但我无法在其中使用OnClickListener。我的代码有什么问题可以告诉我吗?

这是我的功能

private void showInputDialog() {

    final Dialog dialog=new Dialog(MainDashboard.this);
    dialog.setContentView(R.layout.frg_dialog_change_pass);

    btn_save_password=(Button) findViewById(R.id.btn_save_password);
    btn_cancel_pass=(Button) findViewById(R.id.btn_cancel_pass);
    edtOldpass=(EditText) findViewById(R.id.edtOldpass);
    edtNewpass=(EditText) findViewById(R.id.edtNewpass);
    edtConfirmpass=(EditText)findViewById(R.id.edtConfirmpass);

    dialog.show();///Show the dialog.

    btn_save_password.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });
}

3 个答案:

答案 0 :(得分:4)

致电Activity.findViewById()会在View的布局中找到Activity({1}},通过在setContentView()中致电onCreate()来设置View )。

我猜这些Dialog位于您的findViewById()布局中,因此您需要在Dialog个实例上调用btn_save_password = (Button) dialog.findViewById(R.id.btn_save_password); btn_cancel_pass = (Button) dialog.findViewById(R.id.btn_cancel_pass); edtOldpass = (EditText) dialog.findViewById(R.id.edtOldpass); edtNewpass = (EditText) dialog.findViewById(R.id.edtNewpass); edtConfirmpass = (EditText) dialog.findViewById(R.id.edtConfirmpass);

this.myAsyncMethod((b) => {
  this.c = String(b);
});

答案 1 :(得分:0)

// Declare this globally above oncreate
private android.app.AlertDialog dialog;

android.app.AlertDialog.Builder alertDialog = new android.app.AlertDialog.Builder(MainDashboard.this);

        LayoutInflater layoutInflater = getLayoutInflater();
        View alertView = layoutInflater.inflate(R.layout.frg_dialog_change_pass, null);
        alertDialog.setView(alertView);
        alertDialog.setCancelable(false);

        Button btn_save_password= (Button) alertView.findViewById(R.id.btn_save_password);

        btn_save_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do your stuff here
            }
        });

        if(dialog !=null)
            dialog.dismiss();

        dialog = alertDialog.create();
        dialog.show();

答案 2 :(得分:0)

将您的功能更改为:

private void showInputDialog() {

    final Dialog dialog=new Dialog(MainDashboard.this);
    View view = LayoutInflater.from(MainDashboard.this).inflate(R.layout.frg_dialog_change_pass);
    dialog.setContentView(view);
    btn_save_password=(Button) view.findViewById(R.id.btn_save_password);
    btn_cancel_pass=(Button) view.findViewById(R.id.btn_cancel_pass);
    edtOldpass=(EditText) view.findViewById(R.id.edtOldpass);
    edtNewpass=(EditText) view.findViewById(R.id.edtNewpass);
    edtConfirmpass=(EditText)view.findViewById(R.id.edtConfirmpass);
    dialog.show();///Show the dialog.
    btn_save_password.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainDashboard.this, "Success", Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });
}

基本上,您必须将findViewByIdview dialog一起使用。