developer.android.com上的editText.setOnEditorActionListener错误

时间:2014-06-21 03:05:32

标签: java android

我在developer.android.com上看到此代码用于处理软键盘的IME_ACTION:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

如果你看到,这段代码无效,因为mehod OnEditorActionListener用于TextView类型......

那么,有什么方法可以使用软键盘的IME_ACTION?

3 个答案:

答案 0 :(得分:3)

请改用ButterKnife

@OnEditorAction(R.id.editText)
protected boolean actionDo(int actionId){
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doCalculate();
        return true;
    }
    return false;
}

有效!

答案 1 :(得分:2)

您需要在响应操作之前指定imeOption

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

然后您可以使用以下代码回复它:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

答案 2 :(得分:0)

你应该对onEditorAction方法返回false。