使用 AlertDialog.Builder 显示软键盘(数字)

发布于 2024-12-06 18:36:28 字数 1805 浏览 0 评论 0 原文

我在 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>

I have an EditText in an AlertDialog, but when it pops up, I have to click on the text box before the keyboard pops up. This EditText is declared in the XML layout as "number", so when the EditText is clicked, a numeric keypad pops up. I want to eliminate this extra tap and have the numeric keypad pop up when the AlertDialog is loaded.

All of the other solutions I found involve using

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

This is NOT an acceptable solution, as this results in the standard keyboard rather than the numeric keyboard popping up. Does anyone have a way to make the numeric keyboard pop up in an AlertDialog, preferably while keeping my layout defined in 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();

Edit: I want to be perfectly clear that my layout already has:

android:inputType="number"
android:numeric="integer"

I also tried this:

//...
.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();

but that also did not work. With the setSoftInputMode line, I still get the full keyboard when the AlertDialog loads; without it, I still get nothing. In either case, tapping on the text box will bring up the numeric keypad.

Edit Again:

This is the XML for the EditText

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小清晰的声音 2024-12-13 18:36:28

来得有点晚了,但今天我也遇到了同样的问题。这就是我解决它的方法:

当对话框打开时调用键盘,就像您所做的那样:

 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

现在,进入对话框,我假设您有一个仅接受数字的 EditText 字段。只需请求将焦点放在该字段上,标准键盘就会自动转换为数字键盘:

final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();

现在,您只需记住在完成对话框后关闭键盘即可。只需将其放入正/负/中性按钮点击监听器中即可:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);

Coming a bit late, but today I had this same problem. This is how I solved it:

Call the keyboard when the Dialog opens just like you did:

 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Now, into the Dialog I'm assuming you have a EditText field that accepts only numbers. Just request focus on that field and the standard keybord will automatically transform into the numeric keyboard:

final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();

Now you just have to remember to dismiss the keyboard once you're done with the Dialog. Just put this in the positive/negative/neutral button click listener:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);
凉月流沐 2024-12-13 18:36:28

只需尝试使用 setInputType() 设置输入类型。

EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

Just try to set the InputType by using setInputType().

EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
浪菊怪哟 2024-12-13 18:36:28

我认为你应该继续使用 setSoftInputMethod hack,但也向 android 提供你想要数字输入的提示。

使用 xml 布局属性

为此,您可以在 EditText xml 定义中使用多个 xml 属性(请参阅 android:inputType 了解可用选项)

示例:

<EditText android:inputType="phone" ...
<EditText android:inputType="number" ...
<EditText android:inputType="numberSigned" ...
<EditText android:inputType="numberDecimal" ...

您还可以提示 android 显示数字键盘将输入限制为可接受的字符<一href="http://developer.android.com/reference/android/widget/TextView.html#attr_android:numeric" rel="nofollow noreferrer">android:numeric

示例:

<EditText android:numeric="integer" ...
<EditText android:numeric="signed" ...
<EditText android:numeric="decimal" ...

以编程方式

  • 将 EditText.setRawInputType(int) 与常量(例如 TYPE_CLASS_NUMBER)一起使用,您将在 android:inputType

  • 或 TextView.setKeyListener(new NumberKeyListener())

编辑

AlertDialog默认情况下焦点位于肯定按钮上。看来这就是这里造成麻烦的原因。您可以查看这个类似的问题及其答案。

I think that you should keep on using the setSoftInputMethod hack but also provide hints to android that you want a numeric input.

Using xml layout attributes

For this, you can use several xml attributes to your EditText xml definition (see android:inputType for available options)

Examples:

<EditText android:inputType="phone" ...
<EditText android:inputType="number" ...
<EditText android:inputType="numberSigned" ...
<EditText android:inputType="numberDecimal" ...

You can also both hint android to show digital keyboard and restrict input to acceptable characters with android:numeric

Examples:

<EditText android:numeric="integer" ...
<EditText android:numeric="signed" ...
<EditText android:numeric="decimal" ...

Programatically

  • Use EditText.setRawInputType(int) with constants such as TYPE_CLASS_NUMBER you will find in android:inputType

  • or TextView.setKeyListener(new NumberKeyListener())

EDIT

AlertDialog focus by default on the positive button. It seems that it is what is causing trouble here. You may look at this similar question and its answer.

终难愈 2024-12-13 18:36:28

尝试在布局 xml 中为编辑框指定:

<EditText ...>
  <requestFocus/>
</EditText>

Try to specify in layout xml for your edit box:

<EditText ...>
  <requestFocus/>
</EditText>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文