简体   繁体   English

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

[英]Can't add OnClickListener to a Button in a Dialog

I have written a function to handle showing of Dialog but I am not able to use OnClickListener in it.我编写了一个函数来处理Dialog显示,但我无法在其中使用OnClickListener What is wrong with my code can any one tell me?谁能告诉我我的代码有什么问题?

Here is my function这是我的功能

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();
        }
    });
}

Calling Activity.findViewById() will look for the View in the layout of your Activity (the one you've set by calling setContentView() in onCreate() ).调用Activity.findViewById()将在您的Activity布局中查找View (您通过在onCreate()调用setContentView()设置的那个)。

I guess those View s are in your Dialog layout, so you need to call findViewById() on your Dialog instance:我猜那些View在你的Dialog布局中,所以你需要在你的Dialog实例上调用findViewById()

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);
// 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();

Change your function to:将您的功能更改为:

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();
        }
    });
}

Basically you have to use findViewById with the view which is used for dialog .基本上,您必须将findViewById与用于dialogview使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM