import android.app.Activity;
import android.os.Bundle;
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 implements OnCheckedChangeListener{
private ToggleButton tgbt;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
tgbt = (ToggleButton) findViewById(R.id.toggleButton1);
img = (ImageView) findViewById(R.drawable.ic_launcher);
//给tgbt添加一个监听器
tgbt.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
img.setBackgroundResource(isChecked?R.drawable.abc:R.drawable.ic_launcher);
}
}
- 6 回答
- 0 关注
- 1899 浏览
添加回答
举报
0/150
提交
取消