简体   繁体   English

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

[英]editText.setOnEditorActionListener error on developer.android.com

I see this code on developer.android.com for handle the IME_ACTION of the softkeyboard: 我在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;
    }
});

If you see, this code not working because the mehod OnEditorActionListener is for a TextView type... 如果看到的话,此代码不起作用,因为方法OnEditorActionListener用于TextView类型...

So, what are the right method to haddling the IME_ACTION of the softkeyboard? 那么,什么是避开软键盘的IME_ACTION的正确方法呢?

Please use ButterKnife instead 请改用ButterKnife

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

It works! 有用!

You need to specify imeOption prior to responding action 您需要在响应操作之前指定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" />

And then you can respond to it by using following code: 然后,您可以使用以下代码对其进行响应:

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;
    }
});

您应该向onEditorAction方法返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM