3

My xml code for my Activity is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> 

TO add Key Event for this I have coded in main activity like this

public class MainActivity extends Activity { EditText e1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); e1=(EditText)findViewById(R.id.et1); /* e1.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.i("keycode","Inside on key"); //char a=event.getDisplayLabel(); char a = (char)event.getUnicodeChar(); Log.i("keycode",Character.toString(a)); if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { Log.i("keycode",Integer.toString(keyCode)); Log.i("keycode",(e1.getText()).toString()); return true; } else { return false; } // TODO Auto-generated method stub } });*/ } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub Log.i("key","KeyPressed"+Integer.toString(keyCode)); Log.i("keycode",(e1.getText()).toString()); return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch(keyCode) { case KeyEvent.KEYCODE_A: { Log.i("key1","KeyPressed A"); Log.i("keycode1",(e1.getText()).toString()); return true; } } return super.onKeyUp(keyCode, event); } @Override public void onBackPressed() { // TODO Auto-generated method stub new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Closing Activity") .setMessage("Are you sure you want to close this activity?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNegativeButton("No", null) .show(); } } 

when I press A from the soft Keypad...the method onKeyDown or onKeyUp is Not called...somebody say me the way to invoke these methods when i press alphabet in soft Keypad..

1 Answer 1

6

From http://developer.android.com/reference/android/view/KeyEvent.html

*As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses.*

Thus most keys do not generate any KeyEvent. The only way to capture which key is pressed is through TextWatcher.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.