CheckBox+OnCheckedChangeListener实现复选框切换

效果如图,这是一道心理测试题,可以测试为人

这种效果在界面上用CheckBox实现,逻辑上用OnCheckedChangeListener***给每个复选框监听,监听方法就是在下方输出字符串
xml中我们用线性布局,CheckBox继承自CompoundButton,所以他也是Button的一种,和
Button写法类似

<LinearLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView  android:id="@+id/question" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="假如你在沙漠很口渴,找到一间屋子,摆在你前面的是一块切好的西瓜,一瓶水,一个果冻,你会选择?" app:layout_constraintTop_toTopOf="parent" android:textSize="30sp" />
    <CheckBox  android:id="@+id/watermelon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginTop="20dp" android:text="西瓜">
    </CheckBox>
    <!--id前少写+号,将会导致这个id必须和上面一个CheckBox一样,否则报错,所以要细心-->
    <CheckBox  android:id="@+id/water" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginTop="20dp" android:text="水">
    </CheckBox>
    <CheckBox  android:id="@+id/jelly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginTop="20dp" android:text="果冻">
    </CheckBox>
    <TextView  android:id="@+id/answer" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:textSize="30sp" />

</LinearLayout>

需要指出的是OnCheckedChangeListener 有两种参数的接口
一种是接下来要用到的配CheckBox的
public interface OnCheckedChangeListener {
void onCheckedChanged(CompoundButton var1, boolean var2);
}
另一种是配RadioGroup的,单选框的
public interface OnCheckedChangeListener {
void onCheckedChanged(RadioGroup var1, int var2);
}

package com.example.a18307.link;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    CheckBox checkBox1;
    CheckBox checkBox2;
    CheckBox checkBox3;
    TextView textView;
    private ArrayList<String>strings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//获取界面上的Check和Textview
        checkBox1=(CheckBox)findViewById(R.id.watermelon);
        checkBox2=(CheckBox)findViewById(R.id.water);
        checkBox3=(CheckBox)findViewById(R.id.jelly);
        textView=(TextView)findViewById(R.id.answer);
//给三个按钮分别添加***
        checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener());
        checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener());
        checkBox3.setOnCheckedChangeListener(new OnCheckedChangeListener());

    }
    //这里的OnCheckedChangeListener继承自CompoundButton
    public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            String string="";
            if(checkBox1.isChecked())string=string+","+checkBox1.getText();
            if(checkBox2.isChecked())string=string+","+checkBox2.getText();
            if(checkBox3.isChecked())string=string+","+checkBox3.getText();

//消除第一个逗号,用subString方法取了字符串从第二个字符到最后的内容 
if(string.indexOf(",")==0)string=string.substring(1,string.length());
            textView.setText("你选中了"+ string);
        }
    }

}

最后,如果你选择的是西瓜,那么证明!!

———————————分割线—————————————

你喜欢吃西瓜!

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
2025年初,新的一年开始,我给自己暗暗打气,发誓今年一定要拿到offer。如今2025年即将结束,找工作仍然没有任何水花,如今的失意和落魄和年初信心满满的姿态形成鲜明对比,想必也是因为被社会毒打,认清现实了吧。先分享一下贴主的背景,本人女,本科末流985文科专业,后来保送到华五,成绩一直是班级第一,有过国奖,实习有多段头部大厂经历。发贴的直接原因是今天华为面试挂,在反思中有很多复杂的想法,包括对自身能力的怀疑、对面试官所提问题的不解、对大环境的无奈。贴主是一个说话温柔、不喜欢咄咄逼人、有点社恐的人(基本上算是人们眼中对小女生的刻板印象,所以在历次群面中基本全挂(看到大家争抢当leader、t...
在找内推的小虾米:感觉这一段经历和我好像啊,前段时间面了很多车企,面试项目经历各种被拷打,大多数都没过一面,最有希望拿offer的一个终面挂了把我干破防了,打电话给爸妈哭了一个多小时才缓过来。我也开始否定自己,否定自己的一切,包括性格,能力,成长经历。。。最后面了深圳的某家公司,面试官人都挺友好,提的问题有深度但找到切入点 ,最后hr也按岗位最高的标准给的offer,我才发现自己并没有这么不堪,只是我的能力和经验和之前的岗位要求不那么符合而已。帖主一定不要灰心,招聘的窗口期还有很长很长,保持自信扬长避短,一定有企业能发现你的闪光点,祝好。
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务