Android Layout make all children's not clickable

后端 未结 12 2200
一生所求
一生所求 2021-01-01 12:45

I am using Relative Layout and many buttons in it with TextViews etc.I want to make all of them not clickable unless some event happens.

12条回答
  •  醉酒成梦
    2021-01-01 13:11

    If Butterknife library is used, the views can be grouped and the functionality can be done on the group. Refer http://jakewharton.github.io/butterknife/,

    @BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
    List nameViews;
    

    The apply method allows you to act on all the views in a list at once.

    ButterKnife.apply(nameViews, DISABLE);
    ButterKnife.apply(nameViews, ENABLED, false);
    

    Action and Setter interfaces allow specifying simple behavior.

    static final ButterKnife.Action DISABLE = new ButterKnife.Action() {
      @Override public void apply(View view, int index) {
        view.setEnabled(false);
      }
    };
    
    static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() {
      @Override public void set(View view, Boolean value, int index) {
        view.setEnabled(value);
      }
    };
    

    For eg., if the textviews are grouped, you could do

    static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() {
            @Override public void set(TextView view, Boolean value, int index) {
                view.setClickable(value);
                view.setLongClickable(value);
                if(value){
                    view.setTextColor(color);
                } else {
                    view.setTextColor(color);
                }
            }
        };
    

提交回复
热议问题