控制Android Ok SoftKeyboard按钮

7

当我点击下面显示的EditText时,我会在软件Android键盘上设置一个"OK"键:

 <EditText
            android:id="@+id/rlMP3SeekContainer"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@drawable/text_rougea"
            android:singleLine="true" 
            android:imeOptions="actionDone"
            android:ems="10"
            android:hint="@string/hint_deezer_search"
            android:paddingLeft="@dimen/eight_dp"
            android:paddingRight="@dimen/eight_dp"
            android:textColor="@color/black"
            android:textColorHint="@color/gray_text"
            android:textSize="@dimen/twelve_sp" />

当键盘弹出时,我希望用户在点击“确定”按钮时执行某些操作。但是,我该如何重写键盘上的“确定”按钮以执行我想要的操作?

1个回答

21
你需要实现OnEditorActionListener:
yourEditText.setOnEditorActionListener(
        new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE) {
            /* your code here */
          }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接