onKeyListener不能正常运行的虚拟键盘正常运行、键盘、onKeyListener

2023-09-13 01:01:56 作者:看见岳父只能叫叔叔i

我不明白为什么这片code不工作。只有退格键和返回键检测。监听器不触发任何其他键。我的设备是Nexus One的。

我试图重写活动的的onkeydown方法,而这更是雪上加霜。唯一的检测按钮是硬件后退按钮。

我看到身边一个建议使用TextWatcher和onTextChanged,而可能工作在某些情况下,它不是一个真正的变通。例如,如果文本框为空,则不会检测如果用户preSS退格(删除)按钮。 因此,任何想法?

  TextView的txtInput =(TextView中)findViewById(R.id.txtInput);
    txtInput.setOnKeyListener(新View.OnKeyListener(){
        @覆盖
        公共布尔onKey(视图V,INT关键code,KeyEvent的事件){
            makeToast(键code +键pressed);
            返回true;
        }
    });
 

解决方案

确定。我终于想通了如何做我想做的,我不感到自豪在Android这一点。

我写的服务器/客户端应用程序,其中客户端我都开SoftKeyboard和发送pressed键(字符和DEL键)......每一次关键的是pressed,该字符发送到服务器。如果DEL是pressed我送序列{} BS服务器。

为了做到这一点,我不得不实施TextWatcher和onTextChange效果很好,除非情况时的​​EditText为空,并且用户preSS DEL键。由于在EditText上没有变化就没有办法检测到DEL键pressed。

如何将本机上的文件直接拖到虚拟机上

在除了TextWatcher,我不得不实施onKeyListener我贴在我的EditText控制。这onKeyListener忽略了SoftKeyboard所有键,除了DEL而归。不知道为什么?一个错误可能?

下面是我的code:

  TextView的txtInput =(TextView中)findViewById(R.id.txtInput);
    txtInput.addTextChangedListener(inputTextWatcher);

    txtInput.setOnKeyListener(新View.OnKeyListener(){
        @覆盖
        公共布尔onKey(视图V,INT关键code,KeyEvent的事件){
            Log.d(TAG,关键code +符号(code)派);
            返回false;
        }
    });
 

和TextWatcher ....

 私人TextWatcher inputTextWatcher =新TextWatcher(){
    公共无效afterTextChanged(编辑S){}
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT)
        {}
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
        Log.d(TAG,s.charAt(计数1)+字派);;
    }
};
 

I don't understand why this piece of code is not working. Only backspace and return key are detected. Listener doesn't fire for any other key. My device is Nexus One.

I tried to override activity's OnKeyDown method and that's even worse. The only detected button was hardware back button.

I am seeing around a suggestion to use TextWatcher and onTextChanged, while that might work in some cases, it's not a real work around. For example, if textbox is empty, you won't detect if user press BackSpace(Delete) button. So any ideas?

        TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            makeToast(keyCode + " key pressed");
            return true;
        }
    });

解决方案

Ok. I finally figured how to do what I want, and I am not proud on Android for this.

I am writing server/client application where on client I have to open SoftKeyboard and send pressed keys (characters and DEL key)... Each time key is pressed, that character is sent to server. If DEL is pressed I am sending sequence {BS} to server.

In order to do that I had to implement TextWatcher and onTextChange which works well except for situation when EditText is empty and user press DEL key. Since there is no change in EditText there is no way to detect that DEL key is pressed.

In addition to TextWatcher, I had to implement onKeyListener which I attached to my EditText control. This onKeyListener ignores all keys on SoftKeyboard except DEL and RETURN. Not sure why? A bug maybe?

Here is my code:

    TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.addTextChangedListener(inputTextWatcher);

    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d(TAG, keyCode + " character(code) to send");
            return false;
        }
    });

and TextWatcher....

private TextWatcher inputTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) { }
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
        { }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d(TAG, s.charAt(count-1) + " character to send");;          
    }
};

 
精彩推荐