Android學習筆記——CheckBox

來源:互聯網
上載者:User

標籤:

該工程的功能實現在一個activity中顯示一個單選框和一個多選框

 

以下代碼是MainActivity.java檔案中的代碼

package com.example.checkbox;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {    //對控制項對象進行聲明    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    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //通過控制項的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);        //設定監聽器        gendergroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {                        @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                // TODO Auto-generated method stub                if(femaleButton.getId() == checkedId){                    System.out.println("female");                    Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();                }                else if(maleButton.getId() == checkedId)                {                    System.out.println("male");                    Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();                }                }        });        //為多選按鈕添加監聽器        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                        @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if(isChecked)                {                    System.out.println("swim is checked");                }                else                {                    System.out.println("swim is unchecked");                }            }        });        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                        @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if(isChecked)                {                    System.out.println("run is checked");                }                else                {                    System.out.println("run is unchecked");                }            }        });        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                        @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if(isChecked)                {                    System.out.println("read is checked");                }                else                {                    System.out.println("read is unchecked");                }            }        });    }}

 

以下代碼是activity_main.xml檔案中的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/textView1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <RadioGroup        android: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>        <CheckBox        android:id="@+id/swim"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/swim"        />    <CheckBox        android:id="@+id/run"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/run"        />    <CheckBox        android: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="app_name">CheckBox</string>    <string name="hello_world">Hello world!</string>    <string name="female">女</string>    <string name="male">男</string>    <string name="swim">遊泳</string>    <string name="run">跑步</string>    <string name="read">讀書</string></resources>

 

Android學習筆記——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.