閱讀《Android 從入門到精通》(10)——單項選擇

來源:互聯網
上載者:User

標籤:checked   main   check   src   jsb   down   ted   generate   lmos   

單項選擇(RadioGroup)

RadioGroup 是 LinearLayout 的子類,繼承關係例如以下:

android.view.ViewGroup
android.widget.LinearLayout
android.widget.RadioGroup

RadioGroup 類方法


RadioGroup 示範範例

完整project:http://download.csdn.net/detail/sweetloveft/9402088
下述程式中,主要學習 RadioGroup 的使用方法,須要注意單選button的處理邏輯是通過監聽 RadioGroup 完畢的,詳細處理哪個單選button則是通過 getId() 完畢的。

1.MainActivity.java

package com.sweetlover.activity;import com.sweetlover.radiogroupdemo.R;import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends Activity {private static final int BTN_NUM = 6;private TextView textView = null;private RadioGroup radioGroup = null;private RadioButton[] radioButton = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioButton = new RadioButton[BTN_NUM];textView = (TextView)findViewById(R.id.textView1);radioGroup = (RadioGroup)findViewById(R.id.radioGroup);radioButton[0] = (RadioButton)findViewById(R.id.radioButton1);radioButton[1] = (RadioButton)findViewById(R.id.radioButton2);radioButton[2] = (RadioButton)findViewById(R.id.radioButton3);radioButton[3] = (RadioButton)findViewById(R.id.radioButton4);radioButton[4] = (RadioButton)findViewById(R.id.radioButton5);radioButton[5] = (RadioButton)findViewById(R.id.radioButton6);radioGroup.setOnCheckedChangeListener(new RadioGroupCheckedListener());}class RadioGroupCheckedListener implements OnCheckedChangeListener {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubfor (int i = 0; i < BTN_NUM; i++) {if (checkedId == radioButton[i].getId() && radioButton[i].isChecked()) {CharSequence text = radioButton[i].getText();textView.setText(text);Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();break;}}}}}
2.activity_main.xml

<?

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="30dp" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/system" android:textAppearance="?

android:attr/textAppearanceMedium" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="30dp" > <RadioButton android:id="@+id/radioButton1" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os3" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os4" /> <RadioButton android:id="@+id/radioButton5" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os5" /> <RadioButton android:id="@+id/radioButton6" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/os6" /> </RadioGroup></LinearLayout>

3.string.xml

<resources>    <string name="app_name">RadioGroupDemo</string>    <string name="system">作業系統</string>    <string name="os1">Android</string>    <string name="os2">Sysbian</string>    <string name="os3">WinCE</string>    <string name="os4">PalmOS</string>    <string name="os5">Linux</string>    <string name="os6">iPhoneOS</string></resources>
4.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sweetlover.radiogroupdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="com.sweetlover.activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application></manifest>

閱讀《Android 從入門到精通》(10)——單項選擇

相關文章

聯繫我們

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