EditText soft numeric keyboard sometimes does not allow digits

大憨熊 提交于 2019-12-23 22:17:31

问题


The following code produces an EditText (target version 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.

  1. Click on the field.
  2. The A/N soft keyboard opens up.
  3. Click the 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.
  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.
  8. Clear the field. Type "for?", press 123?, it will not take digits.

I added a TextWatcher. If the digits didn't post, the TextWatcher didn't see them either.

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(""); 

回答1:


EditText is misbehaving because in my custom ViewGroup I had

 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.

Ignore the backspace key.

Randomly ignore numeric keys, but accept the decimal point.

Ignore the newLine(Enter) key

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.




回答2:


You have to add this line in your java code.

bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);



回答3:


Try this line of code .

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


来源:https://stackoverflow.com/questions/33967732/edittext-soft-numeric-keyboard-sometimes-does-not-allow-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!