Android 复选框 以及回显

activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="请选择你喜欢的城市" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:ems="10"
        android:inputType="textPersonName"
        android:text="你喜欢的城市有:" />


    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="北京" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上海" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="杭州" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="三亚" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交" />

</LinearLayout>

MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private CheckBox a,b,c,d;
    private TextView t1,t2;
    private EditText et;
    private Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     

        a=(CheckBox) findViewById(R.id.checkBox);
        b=(CheckBox) findViewById(R.id.checkBox2);
        c=(CheckBox) findViewById(R.id.checkBox3);
        d=(CheckBox) findViewById(R.id.checkBox4);
         t1=findViewById(R.id.textView);
         t2=findViewById(R.id.textView2);
         et=findViewById(R.id.editText);
         b1=findViewById(R.id.button);
         


        CompoundButton.OnCheckedChangeListener lister=new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton ckb, boolean isChecked) {
               if (isChecked){
                   //Toast.makeText(MainActivity.this,t2+ckb.getText().toString(),Toast.LENGTH_LONG).show();
                  et.setText("你选择了:"+ckb.getText());
               }
           }
       };
        a.setOnCheckedChangeListener(lister);
        b.setOnCheckedChangeListener(lister);
        c.setOnCheckedChangeListener(lister);
        d.setOnCheckedChangeListener(lister);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

     //字符串的拼接实现复选框回显     如果  有更好的更简便  不用写那么多if判断语句可以  给小白我说下哦  多谢
                StringBuffer result=new StringBuffer();
                if(a.isChecked()){
                    result.append(a.getText()+",");
                }
                if(b.isChecked()){
                    result.append(b.getText()+",");
                }
                if(c.isChecked()){
                    result.append(c.getText()+",");
                }
                if(d.isChecked()){
                    result.append(d.getText()+",");
                }
                t2.setText(result.toString());
            }
        });
       



    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值