【问题标题】:Catching key pressed with the virtual keyboard in Android?在Android中使用虚拟键盘按下键?
【发布时间】:2010-12-12 21:30:10
【问题描述】:

使用物理键盘,您可以使用KeyListener 捕捉按键,例如:

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

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

【问题讨论】:

  • 你不认为分享你找到的更好的方法会更好吗,我的意思是我来这里寻求帮助,自己找出答案,然后不要放弃答案,更多我认为更少;)

标签: android android-edittext keylistener


【解决方案1】:

到目前为止,我还没有在 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);
    }); 
    }

}

【讨论】:

  • 如果editText中没有输入文字?你认为如何处理删除键?
猜你喜欢
  • 2012-09-30
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 2011-12-29
相关资源
最近更新 更多