How to do something after the user clicks on me EditText

I have an EditText that shows the time. After the user clicks the EditText button, I want to show TimePickerDialog , so I set the View.OnClickListener to my EditText .

But OnClickListener behaves strangely - I touch EditText , and then a soft keyboard appears (which I don't want). When I touch again, OnClickListener.onClick() finally gets called and a dialog box appears.

What if I want the dialog box to appear immediately?

+48
android android-edittext onclicklistener
Jan 22
source share
5 answers

Unlike most other controls, EditText configured when the system is in touch mode. The first click event focuses the control, while the second click event actually fires the OnClickListener . If you disable touch focus using the android:focusableInTouchMode View attribute, OnClickListener should fire as expected.

 <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusableInTouchMode="false" /> 
+98
Feb 17 '10 at 22:39
source share

Another solution is to use ontouchlistener :

 edittext.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(MotionEvent.ACTION_UP == event.getAction()) { mQuaternion_1.setText("" + mQ1); } return true; // return is important... } }); 

If it returns true , the event is processed and the keyboard will not pop up. If you want the keyboard to still pop up and register, you should return it false .

+35
Aug 28 '12 at 17:38
source share

It looks like you do not want the user to really be able to enter EditText text. You just want them to be able to choose a time through a timer. So why not just the button that pops up TimePickerDialog? You can display the time selected in the TextView.

Or you could just replace the EditText view with a TimePicker (not a dialog box, just a regular view).

+3
Jan 22
source share

I solved this using a custom button like this:

 <Button android:id="@+id/btTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="test" android:textSize="20dp" android:background="@android:drawable/edit_text" /> 
+3
Jan 22 '10 at 22:14
source share

If I understood correctly, you just need something like

 <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:inputType="none" /> 

unavailable for editing and available for viewing. Install OnClickListener , and you OnClickListener done. Theoretically, in practice, you should also add

 android:editable="false" 

which is outdated but performs the trick.

+1
Jan 22 '10 at 19:48
source share



All Articles