カスタムレイアウトとEditTextを含むAlertDialog.Builder; ビューにアクセスできません


100

EditTextオブジェクトを使用して警告ダイアログを作成しようとしています。EditTextプログラムで最初のテキストを設定する必要があります。これが私が持っているものです。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null));
EditText editText = (EditText) findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

有効なEditTextオブジェクトを持つために何を変更する必要がありますか?

[編集]

それで、user370305や他の人が私が使うべきだと指摘されました alertDialog.findViewById(R.id.label_field);

残念ながら、ここには別の問題があります。どうやら、コンテンツビューをに設定するAlertDialogと、プログラムが実行時にクラッシュします。ビルダーで設定する必要があります。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

残念ながら、これを行うと、alertDialog.findViewById(R.id.label_field);が返されますnull

[/編集]

回答:


236

editTextalertDialogレイアウトの一部なのでeditText、次を参照してアクセスするだけですalertDialog

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);

更新:

コード行で dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterあるヌルは

以下のようにコードを更新し、各コード行を理解してみてください

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

アップデート2:

Inflaterによって作成されたViewオブジェクトを使用してUIコンポーネントを更新しているので、API 21以降から利用可能なクラスのsetView(int layourResId)メソッドを直接使用AlertDialog.Builderできます。


22
:Uはまた、それを行うことができますdialogBuilder.setView(R.layout.dialog_layout);
SiavA

4
@SiavAこの方法は、利用可能な唯一の形式のAPI 21である
Scaraux

私はダイアログを表示しようとしましたが、RecyclerViewでは機能しませんでしたが、これは機能しました。
Muneeb Mirza

が定義されていないgetLayoutInflater()場合に使用できますinflater
backslashN 2017年

1
@saigopi onClickをオーバーライドすると、引数があります(DialogInterfaceダイアログ、int id)。このonClickメソッドでは、dialog.cancel();を渡すだけです。
Minkoo 2017

28

これを使う

   AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    // Get the layout inflater
    LayoutInflater inflater = (activity).getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the
    // dialog layout
    builder.setTitle(title);
    builder.setCancelable(false);
    builder.setIcon(R.drawable.galleryalart);
    builder.setView(inflater.inflate(R.layout.dialogue, null))
    // Add action buttons
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    }
                }
            });
    builder.create();
    builder.show();

である必要がありbuilder.create().show();、あなたは確認することができbuilder.show();、より詳細のためのコード
ファンティエットヴァンたLiNH

9

あなたは書ける:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

 // ...Irrelevant code for customizing the buttons and title

LayoutInflater inflater = this.getLayoutInflater(); 

View dialogView= inflater.inflate(R.layout.alert_label_editor, null);                    
dialogBuilder.setView(dialogView);

Button button = (Button)dialogView.findViewById(R.id.btnName);

   button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {

         //Commond here......

       }
   });

EditText editText = (EditText)
dialogView.findViewById(R.id.label_field); 

editText.setText("test label"); 

dialogBuilder.create().show();

3

Kotlinで必要な場合:

val dialogBuilder = AlertDialog.Builder(this)
// ...Irrelevant code for customizing the buttons and title
val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
dialogBuilder.setView(dialogView)

val editText =  dialogView.findViewById(R.id.label_field)
editText.setText("test label")
val alertDialog = dialogBuilder.create()
alertDialog.show()

転載user370305さん@答え。


2

これを変える:

EditText editText = (EditText) findViewById(R.id.label_field);

これに:

EditText editText = (EditText)  v.findViewById(R.id.label_field);

1
View v=inflater.inflate(R.layout.alert_label_editor, null);
alertDialog.setContentView(v);
EditText editText = (EditText)v.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

1
/**
 * Shows  confirmation dialog about signing in.
 */
private void startAuthDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    alertDialog.getWindow().setLayout(800, 1400);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.auth_dialog, null);
    alertDialog.getWindow().setContentView(dialogView);
    EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
    editText.setText("test label");
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.