Как добавить TextInput в AlertDialog
Есть TextInput, находящийся в activity_main.xml
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:hint="@string/enter_hint_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/findWordText"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
который надо добавить в AlertDialog, код которого:
@SuppressLint("UseCompatLoadingForDrawables")
public void SelectClick(View view) {
if (hasPermissions()) {
MaterialAlertDialogBuilder alertDialog = new MaterialAlertDialogBuilder(this);
alertDialog.setTitle("Введите значение для поиска");
alertDialog.setIcon(R.drawable.ic_baseline_search_24);
alertDialog.setBackground(this.getDrawable(R.drawable.alert_dialog_bg));
alertDialog.setNeutralButton("Закрыть", (dialog, which) -> alertDialog.setOnCancelListener(DialogInterface::cancel));
alertDialog.setPositiveButton("Поиск", (dialog, which) -> {
reader();
});
alertDialog.show();
} else {requestPermissionWithRationale();}
}
Вызывается AlertDialog после нажатия на FloatingActionBtn.
Как добавить TextInput в AlertDialog? Программно (в java классе) или в xml...?
Ответы (2 шт):
Автор решения: Barmaley
→ Ссылка
Так же как и обычно. Создаете лейаут, в котором есть нужные виджеты, инфлейтите его во View и аттачите его в AlertDialog, приблизительно так:
LayoutInflater layoutInflater=this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setView(view);
Автор решения: Wlad
→ Ссылка
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();