使用RadioGroup控件实现选项选择功能

本文介绍了一个简单的Android应用实例,该应用包含一个用于选择性别的界面。通过TextView展示问题,并利用RadioGroup及RadioButton组件实现选项功能。当用户选择不同的RadioButton时,对应的选择会显示在界面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">test</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="hello"></string>
  <string name="tr_radio_op1">男生</string>
  <string name="tr_radio_op2">女生</string>
  <string name="str_radio_question1">请问你是?</string>
</resources>



<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="228px"
    android:layout_height="49px"
    android:text="@string/str_radio_question1"
    android:textSize="30sp"    
    android:layout_x="37px"
    android:layout_y="3px"
  />   
  <RadioGroup
    android:id="@+id/myRadioGroup"
    android:layout_width="137px"
    android:layout_height="216px"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px" 
    >
    <RadioButton
      android:id="@+id/myRadioButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tr_radio_op1"
    />
    <RadioButton
      android:id="@+id/myRadioButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tr_radio_op2"
    />
    </RadioGroup>    
</AbsoluteLayout>


package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class MainActivity extends Activity {

	public TextView mTextView1;
	  public RadioGroup mRadioGroup1; 
	  public RadioButton mRadio1,mRadio2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView1 = (TextView) findViewById(R.id.myTextView);
        mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
        mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
        mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); 
          
        /*RadioGroup用OnCheckedChangeListener来运行*/ 
        mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
      } 
       
      private RadioGroup.OnCheckedChangeListener mChangeRadio = new 
               RadioGroup.OnCheckedChangeListener()
      { 
        public void onCheckedChanged(RadioGroup group, int checkedId)
        { 
          // TODO Auto-generated method stub 
          if(checkedId==mRadio1.getId())
          { 
            /*把mRadio1的内容传到mTextView1*/
            mTextView1.setText(mRadio1.getText());
          } 
          else if(checkedId==mRadio2.getId()) 
          { 
            /*把mRadio2的内容传到mTextView1*/
            mTextView1.setText(mRadio2.getText()); 
          }       
        } 
      };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值