我有一个EditText字段,输入类型为signed decimal,并为其注册了一个键侦听器。
现在我得到数字和删除的事件,但没有减去和小数点的事件。
如果按下减号或小数点键时如何接收事件?
这是我做的:
在布局(activity_edit.xml
)中:
<EditText
android:selectAllOnFocus="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/amount"
android:inputType="numberSigned|numberDecimal" />
在EditActivity.java
:
protected void onCreate(Bundle stored) {
super.onCreate(stored);
setContentView(R.layout.activity_edit);
//[...]
amount = (EditText) findViewById(R.id.amount);
amount.setOnKeyListener(this);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(LOGTAG, "onKey event: " + event);
return false;
}
当我按下一个数字或删除时,我会用logcat获取这样的事件:
onKey event: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_2, scanCode=0, metaState=0, flags=0x6, repeatCount=0,...
onKey event: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_1, scanCode=0, metaState=0, flags=0x6, repeatCount=0,...
onKey event: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DEL, scanCode=0, metaState=0, flags=0x6, repeatCount=0,...
但是当我按-
或.
答案 0 :(得分:0)
我可以使用TextWatcher
代替OnKeyListener
protected void onCreate(Bundle stored) {
super.onCreate(stored);
setContentView(R.layout.activity_edit);
//[...]
amount = (EditText) findViewById(R.id.amount);
amount.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "Text in amount is " + s.toString());
}