Próbuję utworzyć okno dialogowe ostrzeżenia z EditTextobiektem. Muszę EditTextprogramowo ustawić początkowy tekst . Oto co mam.
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();
Co muszę zmienić, aby mieć prawidłowy EditTextobiekt?
[edytować]
Tak więc user370305 i inni wskazali, że powinienem używać alertDialog.findViewById(R.id.label_field);
Niestety jest tu inny problem. Najwyraźniej ustawienie widoku zawartości na AlertDialogpowoduje awarię programu w czasie wykonywania. Musisz ustawić to na budowniczym.
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();
Niestety, kiedy to zrobisz, alertDialog.findViewById(R.id.label_field);teraz wraca null.
[/edytować]
dialogBuilder.setView(R.layout.dialog_layout);