我有一个仅接受小写字母字符的 EditText,这是我使用 digits
完成的。
<EditText
android:id="@+id/play_edit_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:maxLength="1"
android:inputType="textNoSuggestions"
android:digits="qwertyuiopasdfghjklzxcvbnm"
android:layout_marginTop="100dip"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:imeOptions="actionDone"
/>
旁边还有一个执行操作的按钮。不过,我还希望能够使用键盘上的“Enter”键(由于缺乏更好的名称)来调用该按钮。
progressEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
progressButton.performClick();
return true;
}
return false;
}
});
如果我去掉 android:digits="qwertyuiopasdfghjklzxcvbnm"
行,效果很好。但我想用那条线。也许它不起作用的原因是这些数字中不包含 Enter 键?
问:如果是这样的话,我需要添加什么?否则,我该怎么做?
最佳答案
This works fine if I get rid of the android:digits="qwertyuiopasdfghjklzxcvbnm" line. But I want to use that line.
您应该删除该行,因为它不会执行您认为的操作。它实际上指定 EditText 只接受数字,并且只接受您在值中指定的数字,在您的例子中是 "qwertyuiopasdfghjklzxcvbnm"
,显然没有数字。
另请参阅the docs
android:digits
If set, specifies that this TextView has a numeric input method and that these specific characters are the ones that it will accept. If this is set, numeric is implied to be true. The default is false.
我知道它说的是 TextView,但 EditText 继承了 TextView 的该属性。
您可以做的是检查 @android:inputType
并查看它是否可以为您做任何事情。
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
关于java - 在具有指定数字的 EditText 上使用 OnEditorActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32851414/