简体   繁体   English

如何在触摸时启用editText

[英]How to enable editText on touch

I have an editText which is disabled and I want to enable it when I touch it. 我有一个editText被禁用,我想在触摸它时启用它。

Here is what I've done but it doesn't work : 这是我所做的,但是不起作用:

if(textmail.isEnabled() == false){
        textmail.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                textmail.setEnabled(true);
                return true;
            }


        });
    }

i read your use-case carefully you want to disable your TextView and want to disable it onTouch() ( Correct me if i am wrong ) 我仔细阅读了您的用例,您想要禁用TextView并希望禁用它onTouch()( 如果我输入错了,请纠正我

Here's a solution instead of using setEnabled() as if you set it onTouch() is not called so better to do it manually by declaring a global variable(boolean) in your (View/activity/fragment/class) where you are writing the logic. 这是一个解决方案,而不是使用setEnabled() ,就像您将其设置为onTouch()一样,通过在您的(View / activity / fragment / class)声明编写位置的全局变量(boolean)中调用onTouch()更好地手动完成此操作逻辑。

    //this as global variable
    private boolean isTextViewDisabled=false;


    //place this code on onCreate()
    textmail.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN ){
             if(isTextViewDisabled){
               return false;
              }
            }
          return true;
        }
    });

Set your isTextViewDisabled to true and false when needed as per your use-case. 根据使用情况在需要时将isTextViewDisabled设置为truefalse

But confusion here is condition for enabling and disabling should be something else not the touch event as according to your use case code should be like below: 但是这里的混乱是启用和禁用的条件,应该不是touch event ,根据您的用例代码应如下所示:

   //place this code on onCreate()
    textmail.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN ){
             if(isTextViewDisabled){
               isTextViewDisabled=false;
              }
            }
          return true;
        }
    });

The above code doesn't make a lot of sense until you explain why you want to disable the textView 在您解释了为什么要禁用textView之前,上面的代码没有多大意义。

try this i hope this help 试试这个,我希望这个帮助

        textmail.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {



       if(textmail.isEnabled() == false){
                textmail.setEnabled(true);
            }  
                return true;
            }


        });

If you disable the Edittext, it will not trigger the events ie onTouch/onClick . 如果禁用 Edittext,它将不会触发事件,即onTouch / onClick In order to tackle this issue, you need to avoid using disable and use clickable instead. 为了解决此问题,您需要避免使用禁用,而应使用clickable。 textmail.setClickable(false); within your onCreate method. 在您的onCreate方法中。

To make the component look like it has been disabled, you can also work with the Alpha value where you are to reduce the opacity of the component to make it look faded away. 为了使组件看起来像已被禁用,您还可以使用Alpha值来减少组件的不透明度,使它看起来逐渐消失。

textmail.setAlpha(0.5f);

Now it should response to your listener... 现在它应该响应您的听众...

textmail.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        textmail.setClickable(true);
        textmail.setAlpha(1f);
        return false;
    }
});

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

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