<< Android-Note
ダイアログのボタンの無効化
ダイアログを表示して、なにか操作が完了するまでAlertDialog(アラートダイアログ)下にあるボタンを無効にしたいときは次のようなコードで無効化できます。
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = LayoutInflater.from(this);
    final View dialogView = inflater.inflate(R.layout.my_dlg, null);
    
    builder.setView(dialogView)
        .setTitle(R.string.dlg_title)
        .setPositiveButton(R.string.dlg_ok, 
        new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
            	/*何か処理*/
            }
        });
    AlertDialog dialog = builder.create();
    dialog.show();
    
    Button OKButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        //OKボタンを取得
getButtonに取得したいボタンの定数を渡せばOKです。取得可能なボタンは次の3種類です。
- AlertDialog.ButtonPositive
 - AlertDialog.ButtonNegative
 - AlertDialog.ButtonNeutral
 
ただし、ダイアログをshowメソッドで表示してからでないとこのメソッドは利用できません。
ボタン表示前にボタンを取得しようとするとエラーになるので注意です。
また、このメソッドはAPIレベルが3以上必要です。
© Kaz