//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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。