public class MainActivity extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener{
private ToggleButton tb;
private ImageView img;
protected void Oncreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout1);
tb= (ToggleButton) findViewById(R.id.toggleButton1);
img =(ImageView) findViewById(R.id.imageView1);
tb.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//tb点击时执行方法判断状态,buttonView被点击的控件,isChecked点击状态开关
//tb点击时执行方法判断状态,buttonView被点击的控件,isChecked点击状态开关
img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);//如果为true返回前者,否则返回后者。
}
}
添加回答
举报
0/150
提交
取消