现在的位置: 首页 > 综合 > 正文

Android UI学习笔记 之RadioButton和CheckBox的使用

2014年06月14日 ⁄ 综合 ⁄ 共 5026字 ⁄ 字号 评论关闭

/TextViewUI/res/values/strings.xml

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

    <!-- for radiobutton_ui info display -->
    
    <string name="inputinfo">请输入你的信息</string>
    <string name="gender">性别</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="color">颜色</string>
    <string name="red">红色</string>
    <string name="blue">蓝色</string>
    <string name="green">绿色</string>
    <string name="ok">确定</string>
    
</resources>

/TextViewUI/res/layout/radiobutton_ui.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/inputinfo"
            android:textColor="#f00"
            android:textSize="20sp"
            />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/gender"
            />
        
        <RadioGroup
            android:id="@+id/rg"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
            
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/male"
                android:checked="true"
                />
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/female"
                />
        </RadioGroup>
        </TableRow>
        
       <TableRow>
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/color"
               android:textSize="20sp"
               />
           <LinearLayout
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:orientation="vertical"
               >
            <CheckBox
               android:id="@+id/red_checkbox"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/red"
                />
            <CheckBox
               android:id="@+id/blue_checkbox"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/blue"
                />
            <CheckBox
               android:id="@+id/green_checkbox"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/green"
                />
           </LinearLayout>
       </TableRow>
       
       <TableRow>
           <Button
               android:id="@+id/ok_bt"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:text="@string/ok"
               />
       </TableRow>
       
       <!-- show user's input info -->
       <TextView
           android:id="@+id/show"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           />
       
       
</TableLayout>

/TextViewUI/src/com/example/textviewui/MainActivity.java

package com.example.textviewui;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	//
	private RadioGroup rg;
	private TextView show;
	
	private CheckBox check_red;
	private CheckBox check_blue;
	private CheckBox check_green;
	
	private Button ok_bt;
	private String color_str="";
	private String gender_str="";
	
	//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //test radiobutton changed event
        setContentView(R.layout.radiobutton_ui);
        //
        rg = (RadioGroup)findViewById(R.id.rg);
        show = (TextView)findViewById(R.id.show);
        
        //-----为RadioGroup组件的OnCheck事件绑定监听器
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        	@Override
        	public void onCheckedChanged(RadioGroup group,int checkedId){
        		String tip = checkedId==R.id.male?"boy":"girl";
        		gender_str += tip;
        		//show.setText(tip);
        		
        	}
        });
        
        //
        check_red = (CheckBox)findViewById(R.id.red_checkbox);
        check_blue = (CheckBox)findViewById(R.id.blue_checkbox);
        check_green = (CheckBox)findViewById(R.id.green_checkbox);
        
        /*
        check_red.setOnCheckedChangeListener(new MyCheckBoxChanged());
        check_blue.setOnCheckedChangeListener(new MyCheckBoxChanged());
        check_green.setOnCheckedChangeListener(new MyCheckBoxChanged());
        */
        //improve1
        CompoundButton.OnCheckedChangeListener mychkboxlistner = new MyCheckBoxChanged();
        check_red.setOnCheckedChangeListener(mychkboxlistner);
        check_blue.setOnCheckedChangeListener(mychkboxlistner);
        check_green.setOnCheckedChangeListener(mychkboxlistner);
        
        ok_bt = (Button)findViewById(R.id.ok_bt);
        
        //为按钮点击事件绑定监视器
        ok_bt.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick(View view){
        		show.setText("your info\n" + "gender:" + gender_str + "\ncolor:" + color_str);//1402
        	//	Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
        	}
        });
        //
        
        //
    }
    
    //checkbox event listener  1402
    //为CheckBox组件的OnCheck事件绑定监听器
    private final class MyCheckBoxChanged implements CompoundButton.OnCheckedChangeListener{
    	@Override
    	public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
    		/*
    		if(buttonView.getId() == R.id.red_checkbox){
    			if(isChecked){
    				color_str += buttonView.getText();//14021
    			//	color_str += "red ";//  1402
    			}
    		}
    		if(buttonView.getId() == R.id.blue_checkbox){
    			if(isChecked){
    				color_str += "blue ";//1402
    			}
    		}
    		if(buttonView.getId() == R.id.green_checkbox){
    			if(isChecked){
    				color_str += "green ";//1402
    			}
    		}
    		*/
    		//improve2
    		if(isChecked){
    			color_str += buttonView.getText() + " ";
    		}
    		
    		
    	}
    }
    //
    
}

运行结果:

【上篇】
【下篇】

抱歉!评论已关闭.