切换不会更改textView的可见性

时间:2017-06-29 17:03:33

标签: java android

我的开关不会更改textView的可见性。

在activity_first.xml的属性中,我设置了visibility: none

编辑:所有代码: 编辑:所有代码:

public class FirstActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView tv3;
    private Switch switch1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        tv3 = (TextView)findViewById(R.id.tv3) ;
        switch1 = (Switch) findViewById(R.id.switch1);



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

            switch (buttonView.getId()) {
                case R.id.switch1:
                    if (isChecked == true) {
                        tv3.setVisibility(View.VISIBLE);
                    } else {
                        tv3.setVisibility(View.INVISIBLE);
                    }
                    break;
            }

        }
    }
}

1 个答案:

答案 0 :(得分:2)

1。)将onCheckedChanged移到onCreate

之外

2.。)onCheckedChangedswitch1的关联,因为目前它们没有连接

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

    switch1 = (Switch) findViewById(R.id.switch1);
    switch1.setOnCheckedChangeListener(this);
    // link your listener with your switch1 
}

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

    switch (buttonView.getId()) {
        case R.id.switch1:
            if (isChecked == true) {
                tv3.setVisibility(View.VISIBLE);
            } else {
                tv3.setVisibility(View.INVISIBLE);
            }
            break;
    }
}