Android 复选框 以及回显

Stella981
• 阅读 663

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());
            }
        });
       



    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
android ContextMenu 上下文菜单示例
ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
Wesley13 Wesley13
2年前
android Notification 状态栏通知使用示例
ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_
Wesley13 Wesley13
2年前
android WebView 使用实例
主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"
Stella981 Stella981
2年前
Android toolbar 标题精确居中 不会因返回键偏移
1\.总的布局文件<android.support.design.widget.CoordinatorLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http://schemas.android.com
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Stella981 Stella981
2年前
LISTVIew 加分割线
分割线样式文件:<?xmlversion"1.0"encoding"utf8"?<layerlistxmlns:android"http://schemas.android.com/apk/res/android"<item<
Stella981 Stella981
2年前
Android 控件抖动效果
利用Android自带的动画效果,实现控件的抖动效果,效果资源文件shark.xml<?xmlversion"1.0"encoding"utf8"?<translatexmlns:android"http://schemas.android.com/apk/res/android"android:fr
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"