简体   繁体   中英

EditText soft numeric keyboard sometimes does not allow digits

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

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.

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

bottomT.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

试试这行代码。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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