Android CheckBox和RadioButton控件

2017-10-14  本文已影响0人  百里漫步

下面给出Demo,直观见到两个控件如何使用

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/apple" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"        
            android:text="@string/banana" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/man" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/women" />
        </RadioGroup>
    </LinearLayout>
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements android.widget.RadioGroup.OnCheckedChangeListener{

    private CheckBox cb;
    private RadioGroup rg;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //初始化控件
        cb = (CheckBox) findViewById(R.id.checkBox1);
        rg = (RadioGroup) findViewById(R.id.radioGroup1);
       
       //绑定事件
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            //button 被操作的控件,isChecked为  android:checked="true"
            @Override
            public void onCheckedChanged(CompoundButton button, boolean isChecked) {
                if(isChecked){
                    String text = button.getText().toString();
                    Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
                    Log.i("Tag",text);
                }               
            }
        });     
        rg.setOnCheckedChangeListener(this);
    }
         //radioGroup Radio组,radioId 子控件的ID
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int radioId) {
        switch(radioId){
        case R.id.radioButton1:
            Toast.makeText(this, "男", Toast.LENGTH_SHORT).show();
            break;
        case R.id.radioButton2:
            Toast.makeText(this, "女", Toast.LENGTH_SHORT).show();
            break;
        default :break;
        
        }
        
    }
}

效果图:

t.gif
上一篇下一篇

猜你喜欢

热点阅读