我在 AlertDialog 中有一个 EditText,但是当它弹出时,我必须在键盘弹出之前单击文本框。此 EditText 在 XML 布局中声明为“数字”,因此当单击 EditText 时,会弹出一个数字键盘。我想消除这个额外的点击,并在加载 AlertDialog 时弹出数字小键盘。
我发现的所有其他解决方案都涉及使用
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
这不是一个可接受的解决方案,因为这会导致标准键盘而不是数字键盘弹出。有没有人有办法让数字键盘在 AlertDialog 中弹出,最好同时保持我在 XML 中定义的布局?
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Mark Runs")
.setView(markRunsView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
int numRuns = Integer.parseInt(runs.getText().toString());
// ...
})
.setNegativeButton("Cancel", null)
.show();
编辑:我想非常清楚我的布局已经有:
android:inputType="number"
android:numeric="integer"
我也试过这个:
//...
.setNegativeButton("Cancel", null)
.create();
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_CLASS_NUMBER);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
但这也没有用。使用 setSoftInputMode 行,当 AlertDialog 加载时,我仍然可以使用全键盘;没有它,我仍然一无所获。在任何一种情况下,点击文本框都会弹出数字小键盘。
再次编辑:
这是 EditText 的 XML
<EditText
android:id="@+id/runs_marked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:inputType="number"
android:numeric="integer">
<requestFocus/>
</EditText>