Android应用开发UI(单选框)

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

 
Android应用开发UI(单选框)_第1张图片

 

 

代码如下:

 1. res/values/string.xml



    Android底层是基于什么操作系统?
    单选框RadioButton和RadioGroup
    Windows
    Linux
    Mac OS
    JAVA
    更多详情,可登录http://www.80080088.com了解。

 
 2.res/layout/main.xml







    
   	
    
   	
    
   	
    
   	


 

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();
    }
}

 

你可能感兴趣的:(Android/Ophone)