Button.setClickable(false) is not working

前端 未结 11 2149
無奈伤痛
無奈伤痛 2021-01-02 02:19

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

EDIT:

相关标签:
11条回答
  • 2021-01-02 02:56

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

    0 讨论(0)
  • 2021-01-02 02:58

    I wanted to do it on Spinner, and only this one worked for me:

    spinner.setOnTouchListener { v, event ->
                    return@setOnTouchListener true
                }
    
    0 讨论(0)
  • 2021-01-02 02:59

    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).

    0 讨论(0)
  • 2021-01-02 03:00

    Put setClickable after setOnClickListener

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

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

    0 讨论(0)
  • 2021-01-02 03:03

    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;
    }
    
    0 讨论(0)
  • 2021-01-02 03:05

    Set the click listener to null

    someView.setOnClickListener(null)

    As @Jan notes, the setOnClickListener enables the click listener automatically. Therefore, a null click listener can be set to disable future clicks. After setting the view to a null click listener, there are no adverse effects to future clicks on that view.

    0 讨论(0)
提交回复
热议问题