package com.demon4;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton tb;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.初始化控件
tb=(ToggleButton) findViewById(R.id.toggleButton1);
img=(ImageView) findViewById(R.id.imageView1);
// 2.对tb添加监听事件(选中事件.setOnCheckedChangListener())
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//@Override
public void onCheckedChanged(CompoundButton butonView, boolean isChecked) {
// TODO 自动生成的方法存根
// 3.当点击tb的时候更换img的背景图
tb.setChecked(isChecked);
img.setBackgroundResource(isChecked?R.drawable.On:R.drawable.Off);
}
});
}
}
package com.demon4;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton tb;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.初始化控件
tb=(ToggleButton) findViewById(R.id.toggleButton1);
img=(ImageView) findViewById(R.id.imageView1);
// 2.对tb添加监听事件(选中事件.setOnCheckedChangListener())
tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//@Override
public void onCheckedChanged(CompoundButton butonView, boolean isChecked) {
// TODO 自动生成的方法存根
// 3.当点击tb的时候更换img的背景图
tb.setChecked(isChecked);
img.setBackgroundResource(isChecked?R.drawable.On:R.drawable.Off);
}
});
}
}
大神帮看看哪里错了呢?????写完还对的呢,运行就出错,然后都是差