开发者

Catching key pressed with the virtual keyboard in Android?

开发者 https://www.devze.com 2022-12-10 16:02 出处:网络
With the physical keyboard you can catch key presses with a KeyListener, something like: myEditText.setOnKeyListener(new OnKeyListener() {

With the physical keyboard you can catch key presses with a KeyListener, something like:

myEditText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == Ke开发者_运维知识库yEvent.KEYCODE_ENTER) { 
            /* do something */ 
        }
    }
});

Does anyone know how to do this (or similar) with the virtual keyboard?


So far i haven't found any listener for the virtual keypad in android. I found an alternate solution i.e. i used the TextChanged event to retrieve the value of the keys entered in the Edit Text.

import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ShowKeypad extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            EditText emailTxt = (EditText) findViewById(R.id.editText);

            emailTxt.addTextChangedListener(new TextWatcher()
            {
                    public void  afterTextChanged (Editable s){ 
                            Log.d("seachScreen", "afterTextChanged"); 
                    } 
                    public void  beforeTextChanged  (CharSequence s, int start, int 
                            count, int after)
                    { 
                            Log.d("seachScreen", "beforeTextChanged"); 
                    } 
                    public void  onTextChanged  (CharSequence s, int start, int before, 
                            int count) 
                    { 
                            Log.d("seachScreen", s.toString()); 
                    }

            final TextView tv = (TextView)findViewById(R.id.tv);
    }); 
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消