50

カスタムダイアログには興味がなく、layout_weight = 1で3つのボタンを作成する予定です.コードの下に書いています.それは機能していません.常にはいボタンは私にnullを与えます. このコードの何が問題なのですか?

  AlertDialog dialog= new AlertDialog.Builder(this).create();
            dialog.setIcon(R.drawable.alert_icon);
            dialog.setTitle("title");
            dialog.setMessage("Message");
            dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                                                }
            });
            Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            Log.w("Button",""+yesButton);//here getting null
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
            yesButton.setLayoutParams(layoutParams);
            dialog.show();

よろしく、Android 開発者。

4

4 に答える 4

89

答えについては、こちらをご覧ください: http://code.google.com/p/android/issues/detail?id=6360

コメント #4 にshow()あるように、ボタンにアクセスする前にダイアログを呼び出す必要があります。事前に使用することはできません。準備ができたらすぐにボタンを変更する方法に関する自動ソリューションについては、ミッキーの回答を参照してください

于 2011-01-05T12:50:25.813 に答える
72

これは私のために働く:

AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setMessage(message)
                .setCancelable(true)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            //do smthng
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //do snthn
                    }
                }).create();

        alertDialog.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {                    //
                Button positiveButton = ((AlertDialog) dialog)
                        .getButton(AlertDialog.BUTTON_POSITIVE);
                positiveButton.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.btn_default_holo_dark));

                Button negativeButton = ((AlertDialog) dialog)
                        .getButton(AlertDialog.BUTTON_NEGATIVE);
                positiveButton.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.btn_default_holo_dark));
            }
        });

        alertDialog.show(); 

alertDialog.setOnShowListenerこの順序でのみ、後に呼び出すcreate()

于 2013-01-30T10:16:04.303 に答える
6

ありがとうwieux。しかし、目的を理解する新しい開発者のために、私は以下のコードを書き直しています

AlertDialog dialog= new AlertDialog.Builder(this).create();             
dialog.setIcon(R.drawable.alert_icon);             
dialog.setTitle("title");            
dialog.setMessage("Message");             
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {                 
    @Override                 
    public void onClick(DialogInterface arg0, int arg1) {                                                
    }             
}); 
dialog.show(); 
Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);             
Log.w("Button",""+yesButton); //here getting null             
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);             
yesButton.setLayoutParams(layoutParams);        
于 2011-01-05T13:50:47.420 に答える