您好,在android中使用onKey和onKeyUp/Down事件.
例如,我有一个textview.当用户按任意键我想在textview中显示该字符时,在这种情况下使用哪个事件(上面).
PLEASE explain with EXAMPLE
Run Code Online (Sandbox Code Playgroud)
或者给出一些其他示例来获取关键事件并使用edittext或其他方式进行打印.
提前致谢...
Lab*_*lan 12
如果你在EditText中看这个,最好使用它们
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.v("TAG", "afterTextChanged");
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Log.v("TAG", "beforeTextChanged");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v("TAG", "onTextChanged");
}
});
Run Code Online (Sandbox Code Playgroud)
请参考以下代码
public class Demo extends Activity
{
/**
* Variables & Objects Declaration
*
*/
EditText et;
private static Context CONTEXT;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
et =(EditText)findViewById(R.id.header_text02);
}// end of OnCreate
@Override
public boolean onKeyDown(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
// TODO Auto-generated method stub
Log.v("I am ","KeyDown");
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
et.setText("A");
return true;
}
case KeyEvent.KEYCODE_B:
{
//your Action code
et.setText("B");
return true;
}
// similarly write for others too
}
return true;
}// End of onKeyDown
@Override
public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
// TODO Auto-generated method stub
Log.v("I am ","KeyUp");
et.setText("KeyUp");
return true;
}// End of onKeyUp
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23411 次 |
| 最近记录: |