Estoy intentando crear un diálogo de alerta con un EditTextobjeto. Necesito establecer el texto inicial de la EditTextprogramación. Esto es lo que tengo.
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();
¿Qué necesito cambiar para poder tener un EditTextobjeto válido ?
[editar]
Entonces, el usuario370305 y otros señalaron que debería usar alertDialog.findViewById(R.id.label_field);
Desafortunadamente, hay otro problema aquí. Aparentemente, configurar la vista de contenido en el AlertDialoghace que el programa se bloquee en tiempo de ejecución. Tienes que configurarlo en el constructor.
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();
Desafortunadamente, cuando haces esto, alertDialog.findViewById(R.id.label_field);ahora regresa null.
[/editar]

dialogBuilder.setView(R.layout.dialog_layout);getLayoutInflater()cuandoinflaterno está definido.Usa este
fuente
builder.create().show();, puede consultar elbuilder.show();código para obtener más detallesPuedes escribir:
fuente
En caso de que alguien lo quiera en Kotlin:
Se volvió a publicar la respuesta de @ user370305 .
fuente
Cambia esto:
a esto:
fuente
fuente
fuente