我的开关不会更改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;
}
}
}
}
答案 0 :(得分:2)
1。)将onCheckedChanged
移到onCreate
2.。)onCheckedChanged
与switch1
的关联,因为目前它们没有连接
@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;
}
}