带有Edittext setView的Android AlertDialog Builder在底部显示?
Posted
技术标签:
【中文标题】带有Edittext setView的Android AlertDialog Builder在底部显示?【英文标题】:Android AlertDialog Builder with Edittext setView shows at bottom? 【发布时间】:2015-08-10 11:53:30 【问题描述】:您好,我正在使用没有自定义视图的 AlertDialog,通过以下方式设置列表的数组适配器:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setTitle("Please Select");
String[] listStrings = "string1", "string2";
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
builder.setSingleChoiceItems(arrayAdapter, 0, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog.dismiss();
);
builder.show();
我想附加EditText
用于搜索列表,但EditText
显示在列表末尾。我想要EditText
列表顶部,所以请建议我不要使用自定义布局。
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
我尝试改变builder.setView(input);
的顺序
【问题讨论】:
【参考方案1】: AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(input);
builder.setTitle("Please Select");
String[] listStrings = "string1", "string2";
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
layout.addView(list);
list.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
);
builder.setView(layout);
builder.show();
【讨论】:
【参考方案2】:试试这个
boolean checked = false;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LinearLayout linear = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(parm);
final EditText input = new EditText(MainActivity.this);
builder.setTitle("Please Select");
linear.addView(input);
String[] listStrings = "string1", "string2";
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
linear.addView(list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
CheckedTextView chk = (CheckedTextView) view;
Log.d("", "Checked = " + checked);
checked = checked ? false:true;
chk.setChecked(checked);
);
builder.setView(linear);
builder.show();
【讨论】:
以上是关于带有Edittext setView的Android AlertDialog Builder在底部显示?的主要内容,如果未能解决你的问题,请参考以下文章
如何获取一个AlertDialog中的EditText中输入的内容
自定义的dialog中的EditText无法弹出输入法解决方案