Android 带有文本字段和单选列表的AlertDialog
我有一个Android 带有文本字段和单选列表的AlertDialog,android,android-alertdialog,Android,Android Alertdialog,我有一个AlertDialog,它有两个EditText字段。我想用一个选项列表替换第二个EditText字段,但我似乎无法使它工作。 以下是我目前的代码: //dialog box // Init the dialog object AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this); builder.se
AlertDialog
,它有两个EditText
字段。我想用一个选项列表替换第二个EditText
字段,但我似乎无法使它工作。
以下是我目前的代码:
//dialog box
// Init the dialog object
AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
builder.setTitle("Enter name and type");
// Set up the input
final EditText input_name = new EditText(MyActivity.this);
final EditText input_type = new EditText(MyActivity.this);
// Specify the type of input expected;
input_name.setInputType(InputType.TYPE_CLASS_TEXT);
input_type.setInputType(InputType.TYPE_CLASS_TEXT);
LinearLayout ll=new LinearLayout(MyActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(input_name);
ll.addView(input_type);
ll.addView(input_type_list);
builder.setView(ll);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String description;
String type;
description = input_name.getText().toString();
type = input_type.getText().toString();
// display toast message
Context context = getApplicationContext();
CharSequence text = description + " Added!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
//end display toast message
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
我已经看到了文档中关于单一选项列表的内容,但我也希望有一个文本字段
我的背景是C/C++的,我看到的大多数其他帖子都涉及到在xml
文件中设置布局,而我对这些文件不太熟悉
理想情况下,我想这样做:
final ListView input_type_list = new ListView(MapsActivity.this);
final String[] list_strings = {"a", "b", "c"};
并显示列表,而不是编辑文本输入类型
如果我的问题很幼稚,或者有一种标准的方法我错过了,我很抱歉,但如果有人能帮助我或指出正确的方向,我将不胜感激。您是否考虑过只使用自定义对话框?您所说的单选列表是什么意思?@RohitSharma,这三个选项的列表,我可以选择其中一个,然后为什么不使用呢spinner@RohitSharma,我不知道旋转器是什么,正在检查