在Android中使用虚拟键盘按下了按键?

时间:2009-11-04 03:44:53

标签: android android-edittext keylistener

使用物理键盘,您可以使用KeyListener来抓住按键,例如:

myEditText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) { 
            /* do something */ 
        }
    }
});

有人知道如何使用虚拟键盘执行此操作(或类似操作)吗?

1 个答案:

答案 0 :(得分:24)

到目前为止,我还没有在android中找到任何虚拟键盘的监听器。我发现了一个替代解决方案,即我使用TextChanged事件来检索在编辑文本中输入的键的值。

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);
    }); 
    }

}