Как добавить два поля текстовых полей или представлений в поле AlertDialog?
Я хочу добавить два текстовых поля редактирования в диалоговом окне предупреждения. Так же просто, как и в решении, я пока не смог собрать рабочий. Я не могу установить два (редактировать текст) одновременно.
Прошу прокомментировать, если вы хотите увидеть какой-либо дополнительный код.
alertDialog.setTitle("Values");
final EditText quantity = new EditText(SecondScan.this);
final EditText lot = new EditText(SecondScan.this);
quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Project=arr[0].toString();
Item=arr[1].toString();
alertDialog.setMessage( "Employee No. : " + (Login.user).trim()+
"\nWarehouse : " + (FirstScan.Warehouse).trim()+
"\nLocation : " + (FirstScan.Location).trim()+
"\nProject : " + Project.trim() +
"\nItem : " + Item.trim() +
"\nLot : " + Lot.trim()+
"\n\nQuantity :" );
alertDialog.setView(quantity);
alertDialog.setView(lot);
// the bit of code that doesn't seem to be working.
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//ACTION
}
});
AlertDialog alert = alertDialog.create();
alert.show();
Я хочу, чтобы первый текст редактирования появлялся после лота, а второй после количество, тогда как только один из них, похоже, работает, когда я пытаюсь нажать на оба взгляды.
UPDATE. Как оказалось, на самом деле нет метода добавления более одного представления в диалоговое окно предупреждения без необходимости создания макета для него.
Ответы
Ответ 1
Смотрите Создание пользовательского макета в android.
![enter image description here]()
ИЗМЕНИТЬ
alertDialog.setTitle("Values");
final EditText quantity = new EditText(SecondScan.this);
final EditText lot = new EditText(SecondScan.this);
quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Project=arr[0].toString();
Item=arr[1].toString();
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(quantity);
ll.addView(lot);
alertDialog.setView(ll);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//ACTION
}
});
AlertDialog alert = alertDialog.create();
alert.show();
Ответ 2
Я использовал LinearLayout для всплывающего окна входа:
public final String POPUP_LOGIN_TITLE="Sign In";
public final String POPUP_LOGIN_TEXT="Please fill in your credentials";
public final String EMAIL_HINT="--Email--";
public final String PASSWORD_HINT="--Password--";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(POPUP_LOGIN_TITLE);
alert.setMessage(POPUP_LOGIN_TEXT);
// Set an EditText view to get user input
final EditText email = new EditText(this);
email.setHint(EMAIL_HINT);
final EditText password = new EditText(this);
password.setHint(PASSWORD_HINT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(email);
layout.addView(password);
alert.setView(layout);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
Ответ 3
Вам следует создать вертикальный LinearLayout, на котором вы можете добавить свои EditTexts. Затем используйте функцию alertDialog.setView() с LinearLayout.
Посмотрите здесь информацию о mor: Как реализовать пользовательский вид AlertDialog View
или здесь Как добавить два текстовых поля редактирования в диалоговом окне предупреждений
Ответ 4
Почему вы не делаете для него полностью настраиваемый макет?
Вот пользовательский всплывающий я использую для отображения списка категорий и позволяя пользователю выбрать один.
public class CategoryPickerFragment extends DialogFragment implements OnItemClickListener{
private CategoryReceiver receiver;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View view = inflater.inflate(R.layout.category_picker_fragment, null);
builder.setView(view);
AlertDialog ad = builder.create();
CategoryList categoryList = (CategoryList) view.findViewById(R.id.clCategories);
categoryList.setOnItemClickListener(this);
return ad;
}
public void setCategoryReceiver(CategoryReceiver receiver){
this.receiver = receiver;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Category category = ((CategoryListChild)view).getCategory();
receiver.setCategory(category);
this.dismiss();
}
Обратите внимание, что я расширяю DialogFragment, переопределяю OnCreateDialog alertDialog с помощью настраиваемого макета, а затем показываю его пользователю.