android基礎四之RadioGroup

//activity_main.xml
<LinearLayout 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="com.vincentlin.radiogroup.MainActivity" >
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/male" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />
    </RadioGroup>
</LinearLayout>
//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">RadioGroup</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="male">男</string>
    <string name="female">女</string>
</resources>
//MainActivity.java
package com.vincentlin.radiogroup;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements OnCheckedChangeListener{
 private RadioGroup rg;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  rg = (RadioGroup) findViewById(R.id.radioGroup1);
  //實現RadionGroup的事件監(jiān)聽
  rg.setOnCheckedChangeListener( this);
 }
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
  switch (checkedId) {
  case R.id.radio0:
   Log.i("tag","當前為男性");
   break;
  case R.id.radio1:
   Log.i("tag","當前為女性");
   break;
  default:
   break;
  }
  
 }
 
}

分享標題:android基礎四之RadioGroup
當前URL:http://muchs.cn/article30/pphopo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站設計、服務器托管、網站建設App設計、標簽優(yōu)化、網站收錄

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站網頁設計