android学习日常3-15 关于单选框附加监听的复习

MainActivity中代码

package com.mz.dx;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private RadioGroup sex_radioGroup;
    private Button btn_s;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        sex_radioGroup=(RadioGroup)findViewById(R.id.sex_ridioGroup);
        sex_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
                if(arg1==R.id.sex_man){
                    Toast toast=Toast.makeText(MainActivity.this, "您选中的是男", Toast.LENGTH_LONG);
                    toast.show();
                }else{
                    Toast toast=Toast.makeText(MainActivity.this, "您选中的是女", Toast.LENGTH_LONG);
                    toast.show();
                }

            }
        });
        btn_s=(Button)findViewById(R.id.btn_s);
        btn_s.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                for(int i=0;i<sex_radioGroup.getChildCount();i++){
                    RadioButton radio=(RadioButton)sex_radioGroup.getChildAt(i);
                    if(radio.isChecked()){
                        Toast toast=Toast.makeText(MainActivity.this,"您选中的是:"+radio.getText(),Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }

            }
        });

    }
}

前端代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择性别"/>
   <RadioGroup 
       android:id="@+id/sex_ridioGroup"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <RadioButton 
           android:id="@+id/sex_man"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="男"/>
       <RadioButton 
           android:id="@+id/sex_woman"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="女"/>

   </RadioGroup>
   <Button 
       android:id="@+id/btn_s"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="提交"/>

</LinearLayout>

解析未完。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值