当前位置:网站首页>Android 复选框 以及回显
Android 复选框 以及回显
2020-11-09 10:56:00 【osc_r94nrknb】
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());
}
});
}
}
版权声明
本文为[osc_r94nrknb]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4281394/blog/4708767
边栏推荐
- 彩虹排序 | 荷兰旗问题
- Service grid is still difficult - CNCF
- A bunch of code forgot to indent? Shortcut teach you carefree!
- SHOW PROFILE分析SQL语句性能开销
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:樊文凯
- 亚马逊的无服务器总线EventBridge支持事件溯源 - AWS
- 卧槽,这年轻人不讲武德,应届生凭“小抄”干掉5年老鸟,成功拿到字节20Koffer
- python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
- object
- Apache Iceberg 中三种操作表的方式
猜你喜欢
百亿级数据分表后怎么分页查询?
ThinkPHP门面源码解析
[Python from zero to one] 5. Detailed explanation of beautiful soup basic syntax of web crawler
OSChina 周一乱弹 —— 程序媛的青春
The vowels in the inverted string of leetcode
1. What does the operating system do?
失业日志 11月5日
After Android solves the setrequested orientation, the rotation of the mobile phone screen does not trigger the onconfigurationchanged method
作业2020.11.7-8
ThinkPHP框架执行流程源码解析
随机推荐
2020.11.07面试总结(面试12k)
BIOS of operating system
2 normal mode
Commodity management system -- the search function of SPU
5年程序员面试,常见面试问题解析
[QT] subclass qthread to realize multithreading
你不好奇 CPU 是如何执行任务的吗?
无法启动此程序,因为计算机中丢失 MSVCP120.dll。尝试安装该程序以解决此问题
Deng Junhui's notes on data structure and algorithm learning - Chapter 9
[Python从零到壹] 五.网络爬虫之BeautifulSoup基础语法万字详解
推荐系统,深度论文剖析GBDT+LR
彩虹排序 | 荷兰旗问题
常见特征金字塔网络FPN及变体
【QT】子类化QThread实现多线程
A bunch of code forgot to indent? Shortcut teach you carefree!
1450. 在既定时间做作业的学生人数
Android emulator error: x86 emulation currently requires hardware acceleration solution
商品管理系统——SPU检索功能
C++在C的基础上改进了哪些细节
1450. Number of students doing homework at a given time