Android studio 开关 setOnCheckedChangeListener 参数

Android studio Switch setOnCheckedChangeListener parameter

============================================= ============================== 谢谢您的回答!我更改了我的代码并添加了一些 TextView 来检查我的编码是否有效(如果变量更改正确),但我在 "LED in/off" "Relay" 和 "LED blink" 下看不到任何值。这有什么问题? :(

private TextView showpin13, showpin5, showLEDblink;


private String pin13 = "";
private String pin5 = "";
private String LEDblink = "";

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

    switchPin13 = (Switch) findViewById(R.id.changeLED1);
    switchBlinkPin13 = (Switch) findViewById(R.id.changeLED2);
    switchPin5 = (Switch) findViewById(R.id.changeRELAY);
    viewBlink = (TextView) findViewById(R.id.textView7);

    //임시
    showpin13 = (TextView) findViewById(R.id.showpin13);
    showpin5 = (TextView) findViewById(R.id.showpin5);
    showLEDblink = (TextView) findViewById(R.id.showLEDblink);


    viewBlink.setVisibility(View.INVISIBLE);
    switchBlinkPin13.setVisibility(View.INVISIBLE);


    switchPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (switchPin13.isChecked()) {
                switchBlinkPin13.setVisibility(View.VISIBLE);
                viewBlink.setVisibility(View.VISIBLE);
                pin13 = "1";
            } else {
                pin13 = "0";
                viewBlink.setVisibility(View.INVISIBLE);
                switchBlinkPin13.setVisibility(View.INVISIBLE);
                switchBlinkPin13.setChecked(false);
            }
        }
    });

    switchPin5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (switchPin5.isChecked()) {
                pin5 = "1";
            } else {
                pin5 = "0";
            }
        }
    });

    switchBlinkPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(switchBlinkPin13.isChecked()){
                LEDblink = "1";
            }
            else{
                LEDblink = "0";
            }
        }
    });


    String pinnumber13 = pin13;

    showpin13.setText(String.valueOf(pinnumber13));
    showpin5.setText(String.valueOf(pin5));
    showLEDblink.setText(String.valueOf(LEDblink));
}

enter image description here

第一个答案是使 String Pin13 成为全局变量(即在 class 内的 onCreate() 中声明它) 第二点的答案取决于你想完成什么你想把它关掉就是pin.setChecked(false)

  1. Sting pin13放到onCreate()
  2. 外面
  3. else 中添加 switchBlinkPin13.setChecked(false);
String pin13="";//CHANGE HERE
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_status);

switchPin13 = (Switch)findViewById(R.id.changeLED1); //LED switch
switchBlinkPin13 = (Switch)findViewById(R.id.changeLED2); //LED blink
switchPin5 = (Switch)findViewById(R.id.changeRELAY); //Relay switch

switchBlinkPin13.setVisibility(View.INVISIBLE);

final String pin13=""; //or change here 

switchPin13.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(switchPin13.isChecked()){
            switchBlinkPin13.setVisibility(View.VISIBLE);
            pin13 = "1"; //I cannot use pin13 here.
        }
        else {
            pin13 = "0"; //I cannot use pin13 here.
            switchBlinkPin13.setVisibility(View.INVISIBLE);
            //How to set switchBlinkPin13 "OFF"?
        }
    }

但是,您可能希望使用 VIEW.GONE 而不是 INVISIBLE。当你的项目不可见时,它会在你的 LayoutUI 中占用一些 spaces,所以屏幕上会有一个无用的 space。如果你用了GONE(你可以拿回来,不用担心),你就不会面对它。当你的信号灯按钮消失时,你不必担心它是 onClickListener。