標籤: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>
最後在src下的java檔案裡MainActivity.java
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;@Overrideprotected 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);}@Overridepublic 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);}@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {setBulbState(arg1);}}
效果自己檢驗吧!