2016-09-15 21 views
-2

Dialogの表示を処理する関数を作成しましたが、OnClickListenerを使用できません。私のコードで何が間違っているのですか?ダイアログ内のボタンにOnClickListenerを追加できません

は、ここに私の機能(あなたがonCreate()setContentView()を呼び出すことにより、設定した1)あなたのActivityのレイアウトにViewを探しますActivity.findViewById()を呼び出す

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

答えて

4

です。

は、私はそれらの View sがあなたの Dialogレイアウトであると思いますので、あなたの Dialogインスタンスで findViewById()を呼び出す必要があります:基本的にあなたを

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

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

がにあなたの関数を変更しますdialogに使用されるviewfindViewByIdを使用する必要があります。

関連する問題