android中的單選和多選按鈕的使用

1.佈局文件:

 

 

<RadioGroup android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:orientation="vertical"  
            android:id="@+id/radioGroup">  
    <RadioButton android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:id="@+id/radio1"  
                android:text="@string/female"/>  
    <RadioButton android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:id="@+id/radio2"  
                android:text="@string/male"/>  
</RadioGroup>  
<CheckBox android:id="@+id/singBox"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/sing"/>  
<CheckBox android:id="@+id/runBox"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/run"/>  
<CheckBox android:id="@+id/danceBox"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/dance"/>  

 

2.activity

 

package com.example.android1;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.CheckBox;  
import android.widget.CompoundButton;  
import android.widget.LinearLayout;  
import android.widget.RadioButton;  
import android.widget.RadioGroup;  
import android.widget.Toast;  
  
public class LinearLayOut extends Activity  
{  
    private RadioGroup radioGroup;  
    private RadioButton radio1,radio2;  
    private CheckBox runBox,singBox,danceBox;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button button=(Button)findViewById(R.id.button);  
        button.setOnClickListener(new MyButtonListener());//綁定監聽器  
        radio1=(RadioButton)findViewById(R.id.radio1);  
        radio2=(RadioButton)findViewById(R.id.radio2);  
        radioGroup=(RadioGroup)findViewById(R.id.radioGroup);  
        runBox=(CheckBox)findViewById(R.id.runBox);  
        singBox=(CheckBox)findViewById(R.id.singBox);  
        danceBox=(CheckBox)findViewById(R.id.danceBox);  
        //單選按鈕監聽器  
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  
        {  
              
            @Override  
            public void onCheckedChanged(RadioGroup group, int checkedId)  
            {  
                // TODO Auto-generated method stub  
                if(radio1.getId()==checkedId)  
                {  
                    System.out.println("女");  
//                  Toast.makeText(LinearLayout.class, "選擇瞭女", Toast.LENGTH_SHORT).show();  
                    Toast.makeText(LinearLayOut.this, "選擇瞭女", Toast.LENGTH_SHORT).show();  
                }  
                else if(radio2.getId()==checkedId)  
                {  
                    System.out.println("男");  
                }  
            }  
        });  
        //復選框監聽器,每一個checkbox都需要一個  
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
              
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                if(isChecked)  
                {  
                    System.out.println("runBox is selected");  
                }  
                else  
                {  
                    System.out.println("runBox is unselected");  
                }  
                  
            }  
        });  
        singBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
              
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                if(isChecked)  
                {  
                    System.out.println("singBox is selected");  
                }  
                else  
                {  
                    System.out.println("singBox is unselected");  
                }  
                  
            }  
        });  
        danceBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()  
        {  
              
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                if(isChecked)  
                {  
                    System.out.println("danceBox is selected");  
                }  
                else  
                {  
                    System.out.println("danceBox is unselected");  
                }  
                  
            }  
        });  
    }  
    class MyButtonListener implements OnClickListener  
    {  
  
        @Override  
        public void onClick(View v)  
        {  
            Intent intent=new Intent();  
            intent.setClass(LinearLayOut.this, TableLayout.class);  
            LinearLayOut.this.startActivity(intent);  
              
        }  
          

 

 

    }  

}  

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *