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>

解析未完。。。

你可能感兴趣的:(android学习日常3-15 关于单选框附加监听的复习)