Android初級教程小案例之單選框RadioGroup與複選框CheckBox

來源:互聯網
上載者:User

標籤:

Android裡面的單選框和html中的其實是一樣的效果。這裡用到兩個控制項:CheckBox和RadioGroup。直接上代碼:

radio.xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextViewandroid:id="@+id/textView1"      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><RadioGroupandroid:id="@+id/genderGroup"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical"    >    <RadioButton    android:id="@+id/femaleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/female"      />    <RadioButton    android:id="@+id/maleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/male"      /></RadioGroup><CheckBoxandroid:id="@+id/swim" android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="@string/swim"  /><CheckBoxandroid:id="@+id/run" android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="@string/run"  /><CheckBoxandroid:id="@+id/read" android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="@string/read"  /></LinearLayout>


String.xml代碼:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, Activity07!</string>    <string name="app_name">activity07</string>    <string name="male">男</string>    <string name="female">女</string>    <string name="swim">swim</string>    <string name="run">run</string>    <string name="read">read</string></resources>
RadioTest:

package mars.activity07;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioTest extends Activity {    /** Called when the activity is first created. *///對控制項對象進行聲明private RadioGroup genderGroup = null;private RadioButton femaleButton = null;private RadioButton maleButton = null;private CheckBox swimBox = null;private CheckBox runBox = null;private CheckBox readBox = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.radio);        //通過控制項的ID來得到代表控制項的對象        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);        femaleButton = (RadioButton)findViewById(R.id.femaleButton);        maleButton = (RadioButton)findViewById(R.id.maleButton);        swimBox = (CheckBox)findViewById(R.id.swim);        runBox = (CheckBox)findViewById(R.id.run);        readBox = (CheckBox)findViewById(R.id.read);        //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制項的監聽器有所不同        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show();}}});                //為多選按鈕添加監聽器        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked)//未選中到選中狀態是執行這裡:{System.out.println("swim is checked");}else//由選中狀態到未選中狀態時候執行這裡:{System.out.println("swim is unchecked");}}});        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("run is checked");}else{System.out.println("run is unchecked");}}});        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("read is checked");}else{System.out.println("read is unchecked");}}});    }    }


註冊檔案進行註冊:

<application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".RadioTest"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>

執行:



Android初級教程小案例之單選框RadioGroup與複選框CheckBox

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.