【问题标题】:imeoption id not matching with actionidimeoption id 与 actionid 不匹配
【发布时间】:2015-03-10 16:29:54
【问题描述】:

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

    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 语句永远不会为真。

【问题讨论】:

  • Logcat 是否表明它们在您的 if 语句之前是相同的? Log.d(TAG, "IME_ACTION_GO int:"+ IME_ACTION_GO+ " action id: "+ actionId);
  • 在 if 语句之前它们是不一样的。这些值并没有神奇地改变,但从一开始它们就分别是 2 和 0。
  • 所以它为空,这可能会对您有所帮助stackoverflow.com/questions/11301061/…

标签: android


【解决方案1】:

我使用 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;
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2022-12-03
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多