问题
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.
- Click on the field.
- The A/N soft keyboard opens up.
- Click the 123? button at bottom left. The numeric soft keyboard opens up.
- Press any digit. Nothing happens.
- Long press 5, "5/8" gets added into the text field.
- Press any special character, such as @. It might add to the field.
- Clear the field. Type "for", press 123?, now it will take digits.
- 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