简体   繁体   English

EditText 软数字键盘有时不允许数字

[英]EditText soft numeric keyboard sometimes does not allow digits

The following code produces an EditText (target version 23).以下代码生成一个 EditText(目标版本 23)。 I've been working on this for about 8 hours, and have received some suggestions, but I don't think anyone has ever seen this before, so I remain stuck.我已经为此工作了大约 8 个小时,并收到了一些建议,但我认为以前没有人见过这种情况,所以我仍然被卡住了。

  1. Click on the field.单击该字段。
  2. The A/N soft keyboard opens up. A/N 软键盘打开。
  3. Click the 123?单击 123? button at bottom left.左下角的按钮。 The numeric soft keyboard opens up.数字软键盘打开。
  4. Press any digit.按任意数字。 Nothing happens.没发生什么事。
  5. Long press 5, "5/8" gets added into the text field.长按 5,“5/8”被添加到文本字段中。
  6. Press any special character, such as @.按任何特殊字符,例如@。 It might add to the field.它可能会添加到该领域。
  7. Clear the field.清场。 Type "for", press 123?, now it will take digits.输入“for”,按123?,现在需要数字。
  8. Clear the field.清场。 Type "for?", press 123?, it will not take digits.输入“for?”,按123?,不会取数字。

I added a TextWatcher.我添加了一个 TextWatcher。 If the digits didn't post, the TextWatcher didn't see them either.如果数字没有发布,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(""); 

EditText is misbehaving because in my custom ViewGroup I had 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

It's clear now that I can't setRight() and setBottom(), but it's also clear that EditText should not get weird.现在很清楚我不能 setRight() 和 setBottom(),但也很清楚 EditText 不应该变得奇怪。

Ignore the backspace key.忽略退格键。

Randomly ignore numeric keys, but accept the decimal point.随机忽略数字键,但接受小数点。

Ignore the newLine(Enter) key忽略 newLine(Enter) 键

Which keys are ignored, or not, depends on the device.忽略或不忽略哪些键取决于设备。 Samsung Tab 4 or the Nexus 5 API 23 X86 emulator are good places to see this.三星 Tab 4 或 Nexus 5 API 23 X86 模拟器是查看此内容的好地方。

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

bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

试试这行代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM