Android应用开发UI(单选框)

在Android平台上,单项选择的组件,可以通过RadioGroup和RadioButton组合来实现多选一的效果,如图:

 

 

 

代码如下:

 1. res/values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title_txt">Android底层是基于什么操作系统?</string>
    <string name="app_name">单选框RadioButton和RadioGroup</string>
    <string name="windows">Windows</string>
    <string name="linux">Linux</string>
    <string name="mac">Mac OS</string>
    <string name="java">JAVA</string>
    <string name="about_80080088">更多详情,可登录http://www.80080088.com了解。</string>
</resources>

 
 2.res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title_txt"
    android:id="@+id/title_txt"/>
<TextView 
	android:text="@string/about_80080088"
	android:id="@+id/about_80080088" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
</TextView>
<RadioGroup 
	android:id="@+id/rdGroup" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content">
    <RadioButton 
    	android:text="@string/windows"
    	android:layout_height="wrap_content" 
    	android:layout_width="wrap_content" 
    	android:id="@+id/windows" >
   	</RadioButton>
    <RadioButton 
    	android:text="@string/linux"
    	android:layout_height="wrap_content" 
    	android:layout_width="wrap_content" 
    	android:id="@+id/linux">
   	</RadioButton>
    <RadioButton 
    	android:text="@string/mac"
    	android:layout_height="wrap_content" 
    	android:layout_width="wrap_content" 
    	android:id="@+id/mac">
   	</RadioButton>
    <RadioButton 
    	android:text="@string/java"
    	android:layout_height="wrap_content" 
    	android:layout_width="wrap_content" 
    	android:id="@+id/java">
   	</RadioButton>
</RadioGroup>
</LinearLayout>

 

3,Activity代码

package com.cottsoft.android;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class A006Activity extends Activity {
	private TextView m_TextView,m_TextView_About;
	private RadioGroup m_RadioGroup;
	private RadioButton m_Radio1,m_Radio2,m_Radio3,m_Radio4;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_TextView = (TextView)findViewById(R.id.title_txt);
        m_TextView_About = (TextView)findViewById(R.id.about_80080088);
        m_RadioGroup = (RadioGroup)findViewById(R.id.rdGroup);
        m_Radio1 = (RadioButton)findViewById(R.id.windows);
        m_Radio2 = (RadioButton)findViewById(R.id.linux);
        m_Radio3 = (RadioButton)findViewById(R.id.mac);
        m_Radio4 = (RadioButton)findViewById(R.id.java);
        
        m_TextView.setTextSize(25);
        
        m_TextView_About.setTextColor(Color.GRAY);
        m_TextView_About.setTextSize(15);
        
        m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId == m_Radio2.getId()){
					displayToast("正确答案:"+m_Radio2.getText()+",恭喜你,回答正确。");
				}else{
					displayToast("回答错误!");
				}
			}
		});
    }
    
    public void displayToast(String str){
    	Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
    	toast.setGravity(Gravity.TOP, 0, 340);
    	toast.show();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值