标签:android
如图:
点击单选按钮"开灯",多选按钮就会显示"关灯"且方块里有对勾;反之,点多选按钮,单选按钮也自动改变。
首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"bulb",把两张图片:开灯与关灯状态的图片放入"drawable-"随意一个文件夹下
然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="120dp" android:layout_height="120dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/off" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/image" android:layout_below="@+id/image" android:layout_marginRight="35dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <RadioButton android:id="@+id/on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开灯" /> <RadioButton android:id="@+id/off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="关灯" /> </RadioGroup> <CheckBox android:id="@+id/checkBulb11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup1" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="开灯" /> </RelativeLayout>
package com.example.bulb; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.RadioButton; public class MainActivity extends Activity implements OnCheckedChangeListener{ private ImageView image; private RadioButton on,off; private CheckBox checkBulb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); image=(ImageView) this.findViewById(R.id.image); on=(RadioButton) this.findViewById(R.id.on); off=(RadioButton) this.findViewById(R.id.off); checkBulb=(CheckBox) this.findViewById(R.id.checkBulb11); on.setOnCheckedChangeListener(this); checkBulb.setOnCheckedChangeListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void setBulbState(boolean state){ if(state==true){ //改变图片 image.setImageResource(R.drawable.on); //改变checkbox文本 checkBulb.setText("关灯"); }else{ //改变图片 image.setImageResource(R.drawable.off); //改变checkbox文本 checkBulb.setText("开灯"); } //改变radiobutton状态 on.setChecked(state); off.setChecked(!state); //改变chackbox的状态 checkBulb.setChecked(state); } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { setBulbState(arg1); } }
效果自己检验吧!
初识安卓小程序(开关灯——单选多选按钮控制),布布扣,bubuko.com
标签:android
原文地址:http://blog.csdn.net/u011263966/article/details/25424641