【问题标题】:EditText soft numeric keyboard sometimes does not allow digitsEditText 软数字键盘有时不允许数字
【发布时间】:2016-03-02 06:24:01
【问题描述】:

以下代码生成一个 EditText(目标版本 23)。我已经为此工作了大约 8 个小时,并收到了一些建议,但我认为以前没有人见过这个,所以我仍然卡住了。

  1. 单击该字段。
  2. A/N 软键盘打开。
  3. 点击 123?左下角的按钮。数字软键盘打开。
  4. 按任意数字。什么都没发生。
  5. 长按 5,“5/8”会添加到文本字段中。
  6. 按任何特殊字符,例如@。它可能会添加到该字段中。
  7. 清除该字段。输入“for”,按 123?,现在它需要数字。
  8. 清除该字段。输入“for?”,按 123?,不带数字。

我添加了一个 TextWatcher。如果数字没有发布,则 TextWatcher 也看不到它们。

EditText bottomT = new EditText(model);
bottomT.setTextSize(14);
bottomT.setHint("ZIP");  
bottomT.setHintTextColor(Color.BLACK);
bottomT.setBackgroundColor(Color.WHITE);
bottomT.setTextColor(Color.BLACK);
// bottomT.setInputType(InputType.TYPE_CLASS_NUMBER)  Didn't make any difference.
// bottomT.setRawInputType(InputType.TYPE_CLASS_NUMBER)  Didn't make any difference.
// bottomT.setText("", TextView.BufferType.EDITABLE);  DIdn't make a difference
bottomT.setText(""); 

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    EditText 行为不端,因为在我的自定义 ViewGroup 中有

     protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
    ....
         child.layout(child.getLeft(), child.getTop(),
                        child.getLeft() + child.getMeasuredWidth(),
                        child.getTop() + child.getMeasuredHeight());
    
         child.setRight(somevalue);   // CAUSES EDITTEXT PROBLEMS
         child.setBottom(somevalue);  // CAUSES EDITTEXT PROBLEMS
    

    现在很清楚我不能 setRight() 和 setBottom(),但也很清楚 EditText 不应该变得奇怪。

    忽略退格键。

    随机忽略数字键,但接受小数点。

    忽略 newLine(Enter) 键

    忽略或不忽略哪些键取决于设备。 Samsung Tab 4 或 Nexus 5 API 23 X86 模拟器是查看此功能的好地方。

    【讨论】:

      【解决方案2】:

      您必须在您的 java 代码中添加这一行。

      bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
      

      【讨论】:

      • EditorInfo.TYPE_CLASS_NUMBER 和 InputType.TYPE_CLASS_NUMBER 产生相同的结果:出现数字键盘,不接受输入。
      • 但它在我的系统上不起作用。它将接受小数点,但之前或之后没有数字。 EditText 中有一些东西决定是否显示数字 0 - 9。看起来几乎是随机的。
      【解决方案3】:

      试试这行代码。

      bottomT.setInputType(InputType.TYPE_CLASS_NUMBER |InputType.TYPE_NUMBER_FLAG_DECIMAL);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 2012-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-22
        • 2021-08-05
        相关资源
        最近更新 更多