【问题标题】:Android : EditText losing focus after 'Send'Android:“发送”后 EditText 失去焦点
【发布时间】:2015-05-18 03:51:31
【问题描述】:

我正在开发一个消息应用程序,其中我有一个 EditText 供用户键入他的消息。我使用setImeOptions() 方法在键盘上提供了一个“发送”按钮。但是,每当用户点击“发送”按钮时,EditText 就会失去焦点。 (我对“焦点”这个词持怀疑态度,但我的意思是键盘消失了..)

我觉得这有点不方便,因为用户必须在每次发送后再次单击 EditText 才能获取键盘。我在代码中尝试了editText1.requestFocus(),如下所示:

editText1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "send" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    adapter.add(new OneComment(false, editText1.getText().toString()));
                    editText1.setText("");
                    editText1.requestFocus();
                    return true;
                }
                return false;
            }
        });

但这不起作用...请提出解决方法..谢谢:)

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    你可以试试这个

    editText1.setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_SEND){
                editText1.setText("");
                editText1.requestFocus();
                InputMethodManager imm = (InputMethodManager)getSystemService(Service.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText1, 0);
            }
            return true;
        }
    });
    

    【讨论】:

    • 键盘还在消失:(
    • 非常感谢您的快速帮助... :) 但是 Eclipse 显示 getSystemService 方法错误...即使在导入 Service 和 InputMethodManager 之后...任何想法?
    • oops 出错了.. 应该从上下文调用.. 因为这不是这里的上下文.. 检查更新.. 代码是显示软键盘
    • 仍然无法工作..link 会工作吗?
    • yah .. 但那里也写了如何以编程方式显示键盘.. 最后一件事.. 从方法返回 true .. 检查最后的更新... 如果这不起作用,我会给向上 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    相关资源
    最近更新 更多