Android CheckBox和RadioButton控件

下面给出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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,425评论 25 708
  • 其实真的不知道要从那里写起,因为我们的故事有太多了,感觉我们中间分分合合那么多次,最后还是看清了谁才是自己最亲近的...
    歆曦阅读 338评论 0 0
  • 闲暇的午后 午后的阳光 阳光照耀着湖水 湖水倒映着女孩 女孩手里紧握着书 书里讲一个美丽的故事 故事在清冷的空气里...
    婷婷猫阅读 274评论 0 0
  • 一、题目 工程师 M 发明了一种游戏:M 将一个小球随机放入完全相同的三个盒子中的某一个,玩家选中装有球的盒子即获...
    GitHubClub阅读 2,750评论 27 49
  • 昨天,敲完《大东换了牙刷》,大东信誓旦旦地准备通过写他母亲来写“爱恨生死”,今晚坐在电脑前敲字时,大东发现这更是一...
    大东合肥阅读 643评论 0 7

友情链接更多精彩内容