初識安卓小程式(開關燈——單選多選按鈕控制)

來源:互聯網
上載者:User

標籤: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);}}

效果自己檢驗吧!



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.