Button.setClickable(false) is not working

老子叫甜甜 提交于 2019-12-03 20:02:48

问题


I have set mButton.setClickable(false); in my code but still this button is invoked by global button.setOnClickListener of my code.

EDIT: sorry for the delayed update. Below is the details view where I face the issue.
inside my listview customAdapter class getView method

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;
    HashMap<String, Object> cTa= new HashMap<String, Object>();
    cTa= d.getPosition(position)
    Button mButton = (Button)convertView.findViewById(R.id.mBtn);
    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

     if(success)
        {
                    // section-1
            mButton.setClickable(true);
        }
        else{
                   // section-2
            mButton.setClickable(false);
            mButton.setFocusable(false);
        }
    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //operation
        }
    });

    return row;
}

Above is the current code which working,and on section-2 it makes the mButton clickable- false, and focusable - false but still it's listen the below wrapper.getButton().setOnClickListener() and perform the operation. Please suggest me. Sorry for delayed update. Thanks!

UPDATE: I have made below hot-fixes that solve the problem for now.

// section-2
mButton.setVisibility(View.GONE);
mButton.setClickable(false);
mButton.setFocusable(false);

回答1:


That seems to be by design. This is from the documentation of the View.setOnClickListener method:

Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.




回答2:


Instead of using setClickable(false) use setEnabled(false)




回答3:


Instead of using setClickable(false) use following

button.setFocusableInTouchMode(false);

I had the same problem in my app where i needed to set my button not to clickable in certain conditions. this worked for me. Hope this helps.




回答4:


Use View.setOnClickListener() before View.setClickable() ,or the method setOnclickLisnter() will set the flag true.




回答5:


You can check like if(!view.isClickable()) return;




回答6:


This will work in case of Imageview as well as the button.

 private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
        if (imageview.isEnabled()){
            //I have wrapped all code inside onClick() in this if condition
            //Your onClick() code will only execute if the imageview is enabled
            //Now we can use setEnabled() instead of setClickable() everywhere
        }}
    };

Inside onCreate(), you can do setEnabled(false) which will be equivalent to setClickable(false).

We are able to use setEnabled() as tag because it's state remains uneffected on invocation of click (unlike setClickable() whose state changes).




回答7:


Put setClickable after setOnClickListener

mBtn.setOnClickListener(this);
mBtn.setClickable(false);

if you put setClickable(false) before setOnClickListener(this), it doesn't work.




回答8:


I'm not sure if you're still looking for the answer, but for some weird reason

mBtn.setClickable(true);

stops the view from getting clicked and

mBtn.setClickable(false);

makes it clickable again.




回答9:


Like Other friends said, setOnClickListener will override the flag to true.
So the Workaround is to setOnTouchEvent return true whenever you want to disable clicks and set it to retrun false when you want to enable click events.
This is because onTouchEvent is called before every clickListener you define for a view, so returning true will say to all listeners that :

"Ok, I received this event here, nobody else can receive it".

So your solution may be something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;
    HashMap<String, Object> cTa= new HashMap<String, Object>();
    cTa= d.getPosition(position)
    Button mButton = (Button)convertView.findViewById(R.id.mBtn);
    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

     if(success)
        {
                    // section-1
            mButton.setOnTouchListener((v, event) -> false);
        }
        else{
                   // section-2
            mButton.setOnTouchListener((v, event) -> true);
        }
    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //operation
        }
    });
    return row;
}


来源:https://stackoverflow.com/questions/18825747/button-setclickablefalse-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!