imeoption id与actionid不匹配

时间:2015-03-10 16:29:54

标签: android

我正在尝试处理键盘响应:

    EditText editText = (EditText) findViewById(R.id.password);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            Log.d(TAG, "IME_ACTION_GO int:"+ IME_ACTION_GO+ " action id: "+ actionId);
           if (actionId == IME_ACTION_GO) {
                handled = true;
                Toast toast = Toast.makeText(getApplicationContext(),"GO pressed", Toast.LENGTH_SHORT);
                toast.show();
            }
            return handled;
        }
    });

我的布局xml是:

<EditText
    android:id="@+id/password"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:hint= "Password"
    android:inputType="textPassword"
    android:layout_alignBottom="@+id/button"
    android:layout_alignLeft="@+id/user_name"
    android:layout_alignStart="@+id/user_name"
    android:singleLine="true"
    android:imeOptions="actionGo"
    android:imeActionLabel="Login"/>

但是,actionID始终为0,而​​IME_ACTION_GO的int值为2.因此if语句永远不会为真。

1 个答案:

答案 0 :(得分:0)

我使用Keyevent而不是onEditorAction来解决这个问题:

    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            boolean handled = false;
           if (keyCode == KeyEvent.KEYCODE_ENTER) {
                handled = true;
                Toast toast = Toast.makeText(getApplicationContext(),"GO pressed", Toast.LENGTH_SHORT);
                toast.show();
            }
            return handled;
        }
    });