简体   繁体   English

Android EditText字段像按钮

[英]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. 在我的应用程序中,我有一个带有两个EditText字段的活动。

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. 我希望其中一个是标准字段(etNormal),而另一个(etButton)的行为更像是按钮,因此当您触摸它时,不会显示键盘,而是打开了滑动抽屉。 If sliding drawer is opened and you will press the normall edittext sliding drawer will hide. 如果滑动抽屉被打开,您将按普通的edittext滑动抽屉将隐藏。

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. 我曾尝试将条件为如果单击/触摸了etButton打开滑动抽屉,则将OnClickListenerOnTouchListener (不在同一尝试中)添加到这两个条件中(如果没有,则关闭)。

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. 在进行OnTouchListener测试时,它更像是切换,因此当我按下一个抽屉时打开另一个抽屉。 When it came to OnClickListener I needed to press each edtitext twice to get action done. 谈到OnClickListener时,我需要按两次edtitext才能完成操作。

And to hide keybord in etButton I am using setInputType(InputType.TYPE_NULL); 为了在etButton中隐藏键盘,我正在使用setInputType(InputType.TYPE_NULL); . I've tried also setEnabled(false); 我也尝试过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. 当前使用的方法的一个缺陷是,当我将单击从etNormal更改为etButton时,键盘仍然显示,并且没有隐藏。

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的一部分:

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: 同样在XML布局文件的etButton声明中,我有:

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). 但是现在,在etButton上没有碰到任何事情(如果打开了滑动抽屉,则只有其隐藏),并且在触摸etNormal1或2时,滑动抽屉会显示或隐藏,这取决于首先发生的事情(换句话说就是toggel)。

Any idea, what is wrong here? 任何想法,这是怎么了?

Got an editText working like that with 得到了一个editText与

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

Then an OnClickListener to override the action 然后使用OnClickListener覆盖操作

In your layout, add the following attribute to the EditText 在您的布局中,将以下属性添加到EditText

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

Next, write method to handle the click on the EditText and add your application logic . 接下来,编写方法来处理对EditText的单击,并添加您的应用程序逻辑。

In addition to above answers, I hide cursor using cursorVisibility! 除了以上答案外,我还使用cursorVisibility隐藏了光标!

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

cursorVisible To show/hide the cursor on clicking the EditText cursorVisible在单击EditText时显示/隐藏光标

focusable To gain focus when user is touching the view, like EditText focusable在用户触摸视图时获得焦点,例如EditText

focusableInTouchMode To keep a view selected. focusableInTouchMode保持选择视图。 (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. 如果您使用的是onTouch事件,则单击编辑文本时,将获得两个事件,其动作分别为MotionEvent.Action_down和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. 如果etButton需要是一个EditText(为什么不是按钮,如果它的行为像一个按钮?),则可以设置onFocusChangeListener。 Once it gets the focus, you can show the drawer...? 一旦获得焦点,就可以显示抽屉...?

Not sure about not showing the keyboard... 不确定是否不显示键盘...

EditTexts are tricky. EditTexts非常棘手。 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. 使用OnClickListener时必须按两次的原因是,第一次在EditText上获得焦点,并且消耗了touch事件,在这种情况下会触发OnFocusListener。 When you touch the second time, the EditText already has Focus so now a click event is triggered. 当您第二次触摸时,EditText已经具有焦点,因此现在触发了单击事件。

I would suggest you try to do this without EditTexts. 我建议您尝试在没有EditTexts的情况下执行此操作。 That would in any case yield a cleaner and simpler solution. 无论如何,这将产生一个更清洁,更简单的解决方案。 Why exactly do you want to use EditTexts instead of Buttons? 您为什么要使用EditTexts而不是Buttons?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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