如何在多个togglebuttons上设置相同的方法

时间:2017-06-15 09:13:43

标签: android

我已将setOnCheckedChangeListener方法设置为第一个togglebutton,我想用相同的方法设置所有togglebuttons。有人可以帮助我:

public class MainActivity extends AppCompatActivity {

private ToggleButton toggleButton;
private ToggleButton toggleButton2;
private ToggleButton toggleButton3;
private ToggleButton toggleButton4;
private ToggleButton toggleButton5;
private ToggleButton toggleButton6;
private ToggleButton toggleButton7;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
    textView = (TextView) findViewById(R.id.textView);
    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                textView.setText("1");
            } else
                textView.setText("0");
        }
    });
}

}

5 个答案:

答案 0 :(得分:1)

还有一种方法是

public class MainActivity  extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        toggleButton.setOnCheckedChangeListener(this);
toggleButton2.setOnCheckedChangeListener(this);
toggleButton3.setOnCheckedChangeListener(this); //and all others
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            textView.setText("1");
        } else
            textView.setText("0");
    }
    }
}

如果您想要更改单个toggleButton的行为,稍后使用界面的优势,您可以检查它的ID并在覆盖方法中使用不同的任务,如下所示,

public class MainActivity  extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        toggleButton.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        switch (buttonView.getId()){
            case R.id.b1 : someMethod1(); break;
            case R.id.b2 : someMethod2(); break;
        }

    }
}

答案 1 :(得分:0)

创建一个侦听器并将其分配给切换按钮

private CompoundButton.OnCheckedChangeListener onCheckedChange = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(textView != null) {
           if (isChecked) {
               textView.setText("1");
           } else  {
               textView.setText("0");
           }
        }
    }
});

和onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
  textView = (TextView) findViewById(R.id.textView);

  toggleButton.setOnCheckedChangeListener(onCheckedChange);
  toggleButton2.setOnCheckedChangeListener(onCheckedChange);
  ...............
  ..............

答案 2 :(得分:0)

首先:使用一组视图 然后:使用相同的事件功能构建一个函数 然后:同时使用它们

public class MainActivity extends AppCompatActivity {

private ToggleButton[] toggleButtons;

private TextView textView;

private CompoundButton.OnCheckedChangeListene toggleButtonChcekcedEvent()
{
   new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                textView.setText("1");
            } else
                textView.setText("0");
        }
    };
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toggleButtons = new ToggleButton[]
    {
        (ToggleButton) findViewById(R.id.toggleButton),
        (ToggleButton) findViewById(R.id.toggleButton2),
        // please continue to initialize the rest here
    }

    textView = (TextView) findViewById(R.id.textView);

    for (ToggleButton button : toggleButtons)
    {
         button.setOnCheckedChangeListener(toggleButtonChcekcedEvent());
    }


}
}

答案 3 :(得分:0)

如果您在应用程序中使用ButterKnife,则可以像这样处理点击监听器。

  

ButterKnife可以帮助您管理bindView并单击管理器和...

    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'


    @OnClick({R.id.toggleButton1,
        R.id.toggleButton2, 
        R.id.toggleButton3})
     public void clickListener(View view) {   
      switch (view.getId()) {
        case R.id.toggleButton1:
        case R.id.toggleButton2:
        case R.id.toggleButton3:
          //do something here
          break;
     }

答案 4 :(得分:0)

Another approach is to define the handler in xml like this:

<ToggleButton 
  ...
  ...
  android:onClick="onToggleClicked"/>

Then in your Activity:

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
       // Turn on things
    } else {
      // Turn off things
    }
}

This is much DRY and easier to maintain.