简体   繁体   中英

Android EditText field like button

I am trying to cope with one (seemed to be) smalll thing. In my app I've got one activity with two EditText fields.

I want one of them to be normall field (etNormal), and the other (etButton) behave more like button, so when you touch it, the keybord is not shown but instead the sliding drawer is opened. If sliding drawer is opened and you will press the normall edittext sliding drawer will hide.

I've tried adding OnClickListener and OnTouchListener (not in same tries) to both with condition if etButton was clicked/touched open sliding drawer, if not then close.

The outcome was strange. When it was OnTouchListener test it was more like toggle, so when I pressed one drawer opens and on another close. When it came to OnClickListener I needed to press each edtitext twice to get action done.

And to hide keybord in etButton I am using setInputType(InputType.TYPE_NULL); . I've tried also setEnabled(false); but then I was even unable to click/touch it. The one defect of currently used method is when I am changing click from etNormal to etButton, the keyboard is still shown and it doesn't hide.

So, can anyone tell me what I can do to achive my goal?

EDIT:

I've erad your current suggestions and modified a little my code, but still it is not working.

This is a part of it where I am assigning OnTouchListener:

OnTouchListener touchListener = new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                if(v==etButton && ev.getAction()==MotionEvent.ACTION_DOWN) {
                    slidingDrawer.animateOpen();
                }else {
                    slidingDrawer.animateClose();
                }
                return false;
            }
        };

        etNormal1.setOnTouchListener(touchListener);
        etNormal2.setOnTouchListener(touchListener);
        etButton.setOnTouchListener(touchListener);

Also in etButton declaration in XML layout file I have:

android:focusable="false"

But now, on etButton touch nothing hapens (only sliding drawer hides if was opened), and when etNormal1 or 2 is touched sliding drawer shows up or hides depending what was first (in other words toggel).

Any idea, what is wrong here?

Got an editText working like that with

android:focusable="false"
android:clickable="true"

Then an OnClickListener to override the action

In your layout, add the following attribute to the EditText

android:focusable="false"
android:focusableInTouchMode="false"

Next, write method to handle the click on the EditText and add your application logic .

In addition to above answers, I hide cursor using cursorVisibility!

android:focusable="false"
android:clickable="true"
android:cursorVisible="false"
android:focusableInTouchMode="false"

cursorVisible To show/hide the cursor on clicking the EditText

focusable To gain focus when user is touching the view, like EditText

focusableInTouchMode To keep a view selected. (select and click are different)

For detailed understanding, refer here

If you are using onTouch event, when you click the edittext, you will get two events with action as MotionEvent.Action_down and action Up. so basically it will give the effect of clicking the edit text twice. can you please provide the code so that we can have a deep look.

ReWrite your code as :

OnTouchListener touchListener = new OnTouchListener() {

        boolean isOpen=false;

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if(v==etButton && ev.getAction()==MotionEvent.ACTION_UP) {
               if(!isOpen){
                slidingDrawer.animateOpen();
                }else{
                slidingDrawer.animateClose();
                }
                isOpen=!isOpen;
            }
            return false;
        }
    };

If etButton needs to be an EditText (why not a button, if it should behave like one?), maybe you could set an onFocusChangeListener. Once it gets the focus, you can show the drawer...?

Not sure about not showing the keyboard...

EditTexts are tricky. The reason why you have to press twice when you have use an OnClickListener is that the first time around the EditText gets focus and this consumes the touch event, in that case the OnFocusListener is triggered. When you touch the second time, the EditText already has Focus so now a click event is triggered.

I would suggest you try to do this without EditTexts. That would in any case yield a cleaner and simpler solution. Why exactly do you want to use EditTexts instead of Buttons?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM