Android 复选框 以及回显

activity_main.xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 11 <TextView 12 android:id="@+id/textView" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 16 android:text="请选择你喜欢的城市" /> 17 18 <EditText 19 android:id="@+id/editText" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 23 android:ems="10" 24 android:inputType="textPersonName" 25 android:text="你喜欢的城市有:" /> 26 27 28 <CheckBox 29 android:id="@+id/checkBox" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:text="北京" /> 33 34 <CheckBox 35 android:id="@+id/checkBox2" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:text="上海" /> 39 40 <CheckBox 41 android:id="@+id/checkBox3" 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:text="杭州" /> 45 46 <CheckBox 47 android:id="@+id/checkBox4" 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:text="三亚" /> 51 52 <TextView 53 android:id="@+id/textView2" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" 56 android:text="TextView" /> 57 58 <Button 59 android:id="@+id/button" 60 android:layout_width="match_parent" 61 android:layout_height="wrap_content" 62 android:text="提交" /> 63 64</LinearLayout>

MainActivity.java

1package com.example.myapplication; 2 3import androidx.appcompat.app.AlertDialog; 4import androidx.appcompat.app.AppCompatActivity; 5 6import android.content.DialogInterface; 7import android.content.SharedPreferences; 8import android.os.Bundle; 9import android.view.View; 10import android.widget.Button; 11import android.widget.CheckBox; 12import android.widget.CompoundButton; 13import android.widget.EditText; 14import android.widget.TextView; 15import android.widget.Toast; 16 17public class MainActivity extends AppCompatActivity { 18 19 20 private CheckBox a,b,c,d; 21 private TextView t1,t2; 22 private EditText et; 23 private Button b1; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 28 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 33 a=(CheckBox) findViewById(R.id.checkBox); 34 b=(CheckBox) findViewById(R.id.checkBox2); 35 c=(CheckBox) findViewById(R.id.checkBox3); 36 d=(CheckBox) findViewById(R.id.checkBox4); 37 t1=findViewById(R.id.textView); 38 t2=findViewById(R.id.textView2); 39 et=findViewById(R.id.editText); 40 b1=findViewById(R.id.button); 41 42 43 44 CompoundButton.OnCheckedChangeListener lister=new CompoundButton.OnCheckedChangeListener() { 45 46 47 @Override 48 public void onCheckedChanged(CompoundButton ckb, boolean isChecked) { 49 50 51 if (isChecked){ 52 53 54 //Toast.makeText(MainActivity.this,t2+ckb.getText().toString(),Toast.LENGTH_LONG).show(); 55 et.setText("你选择了:"+ckb.getText()); 56 } 57 } 58 }; 59 a.setOnCheckedChangeListener(lister); 60 b.setOnCheckedChangeListener(lister); 61 c.setOnCheckedChangeListener(lister); 62 d.setOnCheckedChangeListener(lister); 63 b1.setOnClickListener(new View.OnClickListener() { 64 65 66 @Override 67 public void onClick(View v) { 68 69 70 71 //字符串的拼接实现复选框回显 如果 有更好的更简便 不用写那么多if判断语句可以 给小白我说下哦 多谢 72 StringBuffer result=new StringBuffer(); 73 if(a.isChecked()){ 74 75 76 result.append(a.getText()+","); 77 } 78 if(b.isChecked()){ 79 80 81 result.append(b.getText()+","); 82 } 83 if(c.isChecked()){ 84 85 86 result.append(c.getText()+","); 87 } 88 if(d.isChecked()){ 89 90 91 result.append(d.getText()+","); 92 } 93 t2.setText(result.toString()); 94 } 95 }); 96 97 98 99 100 } 101}
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

android ContextMenu 上下文菜单示例

ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p

android Notification 状态栏通知使用示例

ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_

android WebView 使用实例

主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"