android toast 和checkbox and radiogroup的使用

1. main.xml 布局文件

<?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" >

    
    <RadioGroup 
        android:id="@+id/groupId"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <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>
    
    <TextView 
        android:id="@+id/love"
        android:text="@string/love"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />
    
    <CheckBox 
        android:id="@+id/swinning"
        android:text="@string/swinning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>
    
    <CheckBox 
        android:id="@+id/running"
        android:text="@string/running"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>
    
    <CheckBox 
        android:id="@+id/reading"
        android:text="@string/reading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CheckBox>

</LinearLayout>

2. Java 调用

package com.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * 常控件的使用
 * @author liuqing
 * @version 1.0
 *
 */
public class ViewCompoentActivity extends Activity {
	
	private RadioGroup radioGroup;
	
	private CheckBox running;
	
	private CheckBox reading;
	
	private CheckBox swinning;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.radioGroup = (RadioGroup)this.findViewById(R.id.groupId);
        this.reading = (CheckBox)this.findViewById(R.id.reading);
        this.swinning = (CheckBox)this.findViewById(R.id.swinning);
        this.running = (CheckBox)this.findViewById(R.id.running);
        //对readioGroup 添加监听器
        this.radioGroup.setOnCheckedChangeListener(
        		new OnCheckedChangeListener(){
            
        	//这里是返加的ID值
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.female) {
					Toast.makeText(getApplication(), "female",
							Toast.LENGTH_SHORT).show();
				}
				else if (checkedId == R.id.male) {
					Toast.makeText(getApplication(), "male", 
							Toast.LENGTH_SHORT).show();
				}
			}
        	
        }
       );
        this.running.setOnCheckedChangeListener 
              (new CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					//如果被选中isChecked 值为true
					if (isChecked) {
						System.out.println("running is true");
						Toast.makeText(getApplication(), 
								"running is true", Toast.LENGTH_SHORT).show();
					}
					else {
						System.out.println("running is false");
					}
					
				}
        	
             }
            );
        
        this.swinning.setOnCheckedChangeListener(
        		new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							System.out.println("swinning is true");
							Toast.makeText(getApplication(), "swinning is true", 
									Toast.LENGTH_SHORT).show();
						}
						else {
							System.out.println("swinning is false");
						}
						
					}
        	
                  }
        		);
        
        this.reading.setOnCheckedChangeListener(
        		new CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (isChecked) {
						System.out.println("reading is true");
						Toast.makeText(getApplication(), "reading is true",
								Toast.LENGTH_SHORT).show();
					}
					else {
						System.out.println("reading is false");
					}
					
				}
        	
               }
        	);
        
    }
}

相关推荐