当我点击一个TextInputEditText时,它是一个LinearLayout的父级,该LinearLayout是以编程方式添加到AlertDialog中的,我的键盘不会显示(在多个设备上测试)。
首先,我创建一个新的LinearLayout,并向其中添加一个新的Spinner。在选择了Spinner上的最后一项后,我将Spinner从LinearLayout中移除并添加一个TextInputEditText:
当我点击
我的
完整代码:
首先,我创建一个新的LinearLayout,并向其中添加一个新的Spinner。在选择了Spinner上的最后一项后,我将Spinner从LinearLayout中移除并添加一个TextInputEditText:
layout.removeAllViews();
layout.addView(input);
当我点击
TextInputEditText
时,它会得到焦点,但没有弹出软键盘。
TextInputEditText
作为View
添加到AlertDialog
中,键盘会弹出并正确显示。我的
AndroidManifest.xml
没有特殊的条目。完整代码:
private void dialogAddContact() {
ArrayList<String> mails = new ArrayList<>();
final LinearLayout layout = new LinearLayout(this);
final TextInputEditText input = new TextInputEditText(this);
final Spinner sp = new Spinner(this);
layout.addView(sp);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
[....]
final ArrayAdapter<String> adp = new ArrayAdapter<>([....]);
sp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(Tools.LOGTAG, position + " " + totalMails);
if(position == totalMails - 1){
/****** Here i remove the spinner and add the input ****/
layout.removeAllViews();
layout.addView(input);
}
}
[....]
});
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout, 50, 0, 50, 0)
[....]
});
dialog = builder.create();
dialog.show();
}