您好,登錄后才能下訂單哦!
//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的事件監聽 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; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。