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); } }
甫艾蒽廷
慕粉3687636
一条小咸鱼
相关分类