Android setOnKeyListener in Dialog

It took me a few tries to get the onKeyListener to work on a dialog EditText. The key is to use View.OnKeyListener() instead of just OnKeyListener().

Here is the code:

		final Dialog newReminderDialog = new Dialog(getActivity());
		newReminderDialog.setContentView(R.layout.dialog_new_reminder);
		newReminderDialog.setTitle("Set Time");
		newReminderDialog.show();
		EditText addToTimeEditText = (EditText) newReminderDialog.findViewById(R.id.addToTimeEntry);
		addToTimeEditText.setOnKeyListener(new View.OnKeyListener(){
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "t", Toast.LENGTH_SHORT).show();
				return true;
			}
			
		});
Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: