设置监听
自己写的第一篇博客,忒有纪念意义啦!

<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:height="50px"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/d1"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/d2"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/d3"
android:text="女"
/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/d4"
android:text="提交"/>
</LinearLayout>
上面是性别选择的代码,如果为男女选择项设置监听,获取我们的选择情况是啥,我们可以写这么一个代码:
RadioGroup sex = (RadioGroup) findViewById(R.id.d1);//获取单选按钮
//为单选按钮设置事件监听器
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton r = (RadioButton) findViewById(checkedId);
Log.i("单选按钮", "您的选择是:" + r.getText());//获取被选中的单选按钮的值并显示出来,显示代码就是Log.i()
}
});
sex相当于一个对象,相当于RadiuGroup监听器的代名词,而d1才是真正的RadiuGroup的对象
posted on 2016-07-30 20:20 finallylove 阅读(152) 评论(0) 收藏 举报
浙公网安备 33010602011771号