捕捉pssed与虚拟键盘的Android按键$ P $? [英] Catching key pressed with the virtual keyboard in Android?
本文介绍了捕捉pssed与虚拟键盘的Android按键$ P $?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用物理键盘,你可以捕捉键presses与的KeyListener ,是这样的:
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 == KeyEvent.KEYCODE_ENTER) {
/* do something */
}
}
});
有谁知道如何与虚拟键盘做到这一点(或类似)?
Does anyone know how to do this (or similar) with the virtual keyboard?
推荐答案
到目前为止,我还没有发现任何侦听器在Android的虚拟键盘。我找到了一个替代的解决方案,即我所用的TextChanged事件来检索编辑的文本输入键的值。
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);
});
}
}
这篇关于捕捉pssed与虚拟键盘的Android按键$ P $?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文