输入Google键盘的按键侦听器不起作用 [英] Enter key listener for Google keyboard is not working

查看:125
本文介绍了输入Google键盘的按键侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不破坏默认换行符的情况下单击Google键盘的输入"键来添加功能.我已经使用过OnKeyListener,但是无法正常工作.

I want to add a functionality on click of "enter" key of Google's keyboard without vanquishing the default newline characteristic. I have used OnKeyListener but it is not working.

      EditText edittext = (EditText) findViewById(R.id.user_query);
      edittext.setOnKeyListener(new OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) {
          if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode ==KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
          //here  i want to print numbers with next line in edit text
          return true;
    }
    return false;
    }
});

相同的代码可与其他键盘一起使用.

The same code is working with other keyboards.

推荐答案

如所述

当硬件键被调用时回调的接口定义 事件调度到此视图.回调将在之前被调用 关键事件被赋予视图.这仅对硬件有用 键盘;软件输入法没有义务触发此操作 听众.

Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

最安全的选择是在以下情况下使用addTextChangedListener

And the most safe bet would be to use addTextChangedListener in your case like below

editText.addTextChangedListener(new TextWatcher() {          
     @Override
     public void onTextChanged(CharSequence s, int start, int before, int count) {                                   
         //here is your code       
    }                       
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
        // TODO Auto-generated method stub                          
    }                       
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub                          
    }
});

这篇关于输入Google键盘的按键侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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