hi 在android中使用onKey和onKeyUp/Down事件的地方。在哪裏&如何在android中使用onKey,onkeyDown,onKeyPressed事件?
例如我有一個textview。當用戶按下任何鍵我想要在文本視圖中顯示該字符, 在這種情況下,使用哪個事件(上圖)。
PLEASE explain with EXAMPLE
或者給一些其他的例子,獲取關鍵事件並在edittext或其他的打印。
在此先感謝...
hi 在android中使用onKey和onKeyUp/Down事件的地方。在哪裏&如何在android中使用onKey,onkeyDown,onKeyPressed事件?
例如我有一個textview。當用戶按下任何鍵我想要在文本視圖中顯示該字符, 在這種情況下,使用哪個事件(上圖)。
PLEASE explain with EXAMPLE
或者給一些其他的例子,獲取關鍵事件並在edittext或其他的打印。
在此先感謝...
請參考下面的代碼
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
}
@shankar可以幫助我在Home按鈕上顯示警報消息。 – Kishore 2011-08-17 06:07:29
如果你在尋找的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");
}
});
最適合我的解決方案! – 2012-03-17 07:01:54
爲了獲得聲譽,讓別人回答你的問題,接受你的問題的好答案。 – 2010-11-23 11:59:27