Reputation: 3713
hi where to use onKey and onKeyUp/Down event in android.
e.g. i have one textview . when user pressed any key i want to display that character in textview, In this case which event(above) is used.
PLEASE explain with EXAMPLE
Or give some other example that get the key event and print in edittext or some other.
Thanks in advance...
Upvotes: 5
Views: 24413
Reputation: 34823
If you are looking this in EditText, its better to use these
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");
}
});
Upvotes: 12
Reputation: 12027
pls refer the following code
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
}
Upvotes: 4