我用EditText有一個簡單的活動。我想在用戶在EditText中輸入名稱並點擊Enter時保存表單數據。當在我的電腦鍵盤上按下Enter時獲取keyCode 0而不是66
除了一件事以外,所有的作品都能找到。 keyCode變量是0而不是66(KeyEvent.KEYCODE_ENTER)。
有沒有人有什麼問題是什麼idéa?
我的佈局XML:
<EditText
android:id="@+id/etName"
style="@style/EditTextFont"
android:inputType="textPersonName" />
我的活動:
private EditText etName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
etName = (EditText) findViewById(R.id.etName);
etName.setOnEditorActionListener(this);
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (v.getId()) {
case R.id.etName:
// actionId will be EditorInfo.IME_NULL if being called
// due to the enter key being pressed.
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(actionId == EditorInfo.IME_NULL)) {
save();
return true;
}
}
return false;
}
問候,馬蒂亞斯
你是什麼意思由keyCode變量是0而不是66?你的代碼工作與否? – 2012-02-17 11:35:35
因爲keyCode爲0,所以if語句中的代碼將不會被執行。當我在EditText中輸入時,onEditorAction方法被調用,唯一的問題是keyCode不是我所期望的。 – Mattias 2012-02-17 12:14:23
Yves Daoust雖然不禮貌,但他的回答實際上是正確的。我混淆了兩種不同的方法。我糾正了上面的解決方案。感謝所有幫助的人。 – Mattias 2012-02-17 14:11:45