处理和QUOT; Enter]键。在果冻豆关键 [英] Handle "Enter" key on Jelly Bean

查看:115
本文介绍了处理和QUOT; Enter]键。在果冻豆关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,在这个应用程序,我有编辑文本。我想,当用户写编辑文本年底一些文本,然后$​​ P $ PSS回车键,我想它调用一些命令。这是我一直在做。这是在ICS的工作,但是当我尝试在其他设备(果冻豆),这是行不通的。

I'm making an application, in this application I have edit text. I want when user write some text in edit text end then press enter button, I want it call some command. This what i have been done. This is work in ICS, but when I try on other device (Jelly Bean) it doesn't work.

inputViaTextChatbot.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                // hide the keyboard  
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                // process 
                getThis = inputViaTextChatbot.getText().toString();
                if (getThis!=null && getThis.length()>1) {  
                    try {
                    Log.v("Got This: ", getThis);
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    inputViaTextChatbot.setText("");  
                }
            }    
            return false;
        }
    });

谁能帮助我做到这一点?

can anyone help me to do this?

推荐答案

这是一个已知的bug,使输入键不能在多个设备识别。一种解决方法,以避免它,并让它工作将是以下内容:

This is a known bug that makes the Enter key not be recognized on several devices. A workaround to avoid it and make it work would be the following:

创建一个 TextView.OnEditorActionListener 是这样的:

TextView.OnEditorActionListener enterKey = new TextView.OnEditorActionListener() {
  public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_GO) {
      // Do whatever you need
    }
    return true;
  }
};

假设你的查看的EditText ,例如,你需要将其设置这种方式:

Assuming your View is an EditText, for instance, you'd need to set it this way:

final EditText editor = (EditText) findViewById(R.id.Texto);
editor.setOnEditorActionListener(enterKey);

最后一步走的是分配以下属性添加到的EditText

android:imeOptions="actionGo"

这基本上改变了回车键的默认行为,将其设置为 actionGo 输入法选项。在您的处理程序只分配它,你所创建的监听器和这种方式,您将有回车键的行为。

This basically changes the default behavior of the enter key, setting it to the actionGo IME option. In your handler simply assign it the listener you've created and this way you'll have the enter key behavior.

这篇关于处理和QUOT; Enter]键。在果冻豆关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆