Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something after user clicks on my EditText

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

But the OnClickListener is behaving weirdly - I touch the EditText and then software keyboard appears (which I don't want). When I touch again, OnClickListener.onClick() is finally called and the dialog appears.

What should I do if I want the dialog to appear immediately?

like image 321
fhucho Avatar asked Jan 22 '10 17:01

fhucho


People also ask

How can I tell if EditText is clicked?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.

How do I scroll up layout when clicking on EditText?

try this android:windowSoftInputMode="adjustResize" in your activity in manifest file.

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

What is the function of setting autoText attribute in EditText?

android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.


2 Answers

Unlike most other controls, EditTexts are focusable while 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-mode focus with the android:focusableInTouchMode View attribute, the 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" /> 
like image 198
Kai Avatar answered Oct 14 '22 08:10

Kai


Another solution is to use the 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 handled and keyboard wont popup. If you'd want the keyboard to still popup and register click you'd have it return false.

like image 23
Warpzit Avatar answered Oct 14 '22 07:10

Warpzit