使用系统自带CheckBox实现单选效果的2个线性布局切换

来源:互联网 发布:网络新词及解释 编辑:程序博客网 时间:2024/05/18 01:10
//安卓开发CheckBox实现单选效果的2个线性布局切换,在宝宝护理和宝妈护理之间切换视图
public class MainActivity extends Activity {
CheckBox checkbox1, checkbox2;
LinearLayout babyInfo, baomuInfo;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
babyInfo = (LinearLayout) this.findViewById(R.id.babyInfo);
baomuInfo = (LinearLayout) this.findViewById(R.id.baomuInfo);
checkbox1 = (CheckBox) findViewById(R.id.checkBox1);
checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {//检查变化监听

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (checkbox1.isChecked()) {
resetStatus();
checkbox1.setChecked(true);
babyInfo.setVisibility(View.VISIBLE);
}
}
});
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (checkbox2.isChecked()) {
resetStatus();
checkbox2.setChecked(true);
baomuInfo.setVisibility(View.VISIBLE);
}
}
});
babyInfo.setVisibility(View.VISIBLE);
}



//将重置布局状态
protected void resetStatus() {
// TODO Auto-generated method stub
checkbox2.setChecked(false);
checkbox1.setChecked(false);
babyInfo.setVisibility(View.GONE);
baomuInfo.setVisibility(View.GONE);
}


}

布局layout代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<LinearLayout 
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
   >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="10dip"
        android:text="类型选择: "
        android:textColor="@color/blue"
        android:textSize="14sp" />


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="宝宝护理" />


    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="宝妈护理" />
</LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="@color/blue"
        android:orientation="vertical" >
    </View>


    <LinearLayout
        android:id="@+id/babyInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:orientation="vertical" >


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="宝宝喂养:指导科学母乳喂养或根据情况进行人工喂养,并记录每次喂养时间与奶量" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="测量体温:每日进行一次体温测量并记录" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="观测呼吸情况:每日记录宝宝1分钟呼吸次数" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="检查尿布:每两个小时常规性检查、更换尿布检查大小便并记录" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="调整卧姿:每两个小时调整一次" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="洗澡时间:每日傍晚给宝宝宝洗澡,包括头部、眼耳鼻清洁" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="脐部护理:宝贝肚脐掉落之前每日用碘伏棉棒消毒清洁" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="抚触:每日宝贝洗澡以前进行抚触" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="婴儿用品消毒:奶瓶、衣物等婴儿常用品清洗高温消毒" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="婴儿床整理消毒:每日整理婴儿床并用酒精擦拭消毒" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="体重身高测量:每周进行一次体重与身高测量并记录" />


        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:background="@color/blue"
            android:orientation="vertical" >
        </View>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/baomuInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:orientation="vertical" >


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="产妇生活护理:科学指导母乳喂养,人工喂养每2~3小时喂养一次 " />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="乳房护理:乳房按摩 疏通 " />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="宝妈擦身:帮助产妇擦洗身体 " />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="营养:配餐指导" />


        <View
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:background="@color/blue"
            android:orientation="vertical" >
        </View>
    </LinearLayout>
</LinearLayout>


1 0
原创粉丝点击