如何确定是否由于鼠标单击或程序的某些更改而检查了开关

时间:2019-01-26 14:31:00

标签: android

我希望在检查_swreceiptqr时将_swreceiptbarcode设置为false,反之亦然。但是如何区分人工触发开关和程序触发开关

   _swreceiptbarcode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

               // _swreceiptbarcode.setChecked(true);
                _swreceiptqr.setChecked(false);

        }
    });

    _swreceiptqr.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                _swreceiptbarcode.setChecked(false);
               // _swreceiptqr.setChecked(true);


        }
    });

4 个答案:

答案 0 :(得分:0)

尝试一下:

  _swreceiptbarcode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if(isChecked){
               // switch button is checked
               // do something
               }else{
               // switch button is not checked
               // do something
               }
    }
});

_swreceiptqr.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked){
               // switch button is checked
               // do something
            }else{
               // switch button is not checked
               // do something
               }

    }
});

答案 1 :(得分:0)

如果您仅将onCreate内的一次开关设置为选中状态,则只能这样做,之后再设置OnCheckedChangedListener

或者,您可以在设置开关的onCheckedChangedListener值时暂时将其设置为null,然后再将其重置。

答案 2 :(得分:0)

伪代码:

boolean iCheckedTheBox=false;

barcode.onChangeCheckedListener(){
      if (iCheckedTheBox){
         iCheckedTheBox = false;
         } else {
         iCheckedTheBox = true;
         if (barcode.isChecked())
            receipt.uncheck();
         else
            receipt.check();
         }
}

receipt.onChangeCheckedListener(){
       if (iCheckedTheBox){
         iCheckedTheBox = false;
         } else {
         iCheckedTheBox = true;
         if (receipt.isChecked())
            barcode.uncheck();
         else
            barcode.check();
         }
}

答案 3 :(得分:0)

在类级别上声明此变量:

private boolean fromCode = false;

这些是听众:

_swreceiptbarcode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (fromCode)
            return;
        fromCode = true;
        _swreceiptqr.setChecked(!isChecked);
        fromCode = false;
    }
});

_swreceiptqr.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (fromCode)
            return;
        fromCode = true;
        _swreceiptbarcode.setChecked(!isChecked);
        fromCode = false;
    }
});

现在,每次您要通过代码设置任何开关的状态时,
您需要这样做:

fromCode = true;
_swreceiptbarcode.setChecked(true);
fromCode = false;

因此它不会触发另一台交换机的侦听器。