[英]custom action button in android keyboard
我正在嘗試在ENTER上執行自定義操作。 這是我的EditText的xml
<EditText
android:id="@+id/editor"
android:singleLine="true"
android:inputType="text"
android:imeActionId="@+id/submit_action"
android:imeActionLabel="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="15dp" />
但是,當我嘗試捕獲該動作時,我看到該動作的ID為5,而不是Submit_action中的值
mNameEditor.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
Toast.makeText(getActivity(), Integer.toString(actionId) ,Toast.LENGTH_LONG).show();
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
return false;
} else if(actionId == R.id.submit_action){
//do something
}
return true;
}
});
您可以使用以下方法:
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
return true;
default:
break;
}
}
return false;
}
在這個例子中,我試圖關閉IME,但是按下ENTER鍵后您可以做任何您想做的事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.