当前位置:网站首页>Android check box and echo
Android check box and echo
2020-11-09 10:56:00 【Number of】
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=" Please choose the city you like " />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text=" Your favorite cities are :" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Beijing " />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Shanghai " />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Hangzhou " />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" sanya " />
<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=" Submit " />
</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(" You chose :"+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) {
// String splicing to achieve check box echo If There are better, simpler Don't write so much if The judgment statement can Let me talk to Xiaobai Thank you very much
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());
}
});
}
}
版权声明
本文为[Number of]所创,转载请带上原文链接,感谢
边栏推荐
- Talk about my understanding of FAAS with Alibaba cloud FC
- 抢球鞋?预测股市走势?淘宝秒杀?Python表示要啥有啥
- 使用CopyMemory API出现 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
- Recommended tools for Mac
- Start learning discrete mathematics again
- Initial installation of linx7.5
- What details does C + + improve on the basis of C
- Front end code style practice prettier + eslint + git hook + lint staged
- 理解Task和和async await
- ThinkPHP框架执行流程源码解析
猜你喜欢
An attempt to read or write to protected memory occurred using the CopyMemory API. This usually indicates that other memory is corrupted.
5 个我不可或缺的开源工具
When Python calls ffmpeg, 'ffmpeg' is not an internal or external command, nor a runnable program
How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
Unemployment log, November 5
JT-day09
After SQL group query, get the first n records of each group
解决python调用 ffmpeg时 ‘ffmpeg‘ 不是内部或外部命令,也不是可运行的程序
Apache Iceberg 中三种操作表的方式
SQL第二章第三章
随机推荐
Impact of libssl on CentOS login
Android 复选框 以及回显
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
财富自由梦缓?蚂蚁金服暂停上市,监管后估值或下跌
AI fresh student's annual salary has increased to 400000, you can still make a career change now!
首次开通csdn,这篇文章送给过去的自己和正在发生的你
How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
After Android solves the setrequested orientation, the rotation of the mobile phone screen does not trigger the onconfigurationchanged method
Sql分组查询后取每组的前N条记录
Commodity management system -- the search function of SPU
上周热点回顾(11.2-11.8)
Python零基础入门教程(01)
Biden wins the US election! Python developers in Silicon Valley make fun of Ku Wang in this way
Using rem, the font size changes when the screen zooms
结合阿里云 FC 谈谈我对 FaaS 的理解
程序人生|从网瘾少年到微软、BAT、字节offer收割机逆袭之路
ubuntu 上使用微信的新方案——手机投屏
Wealth and freedom? Ant financial services suspended listing, valuation or decline after regulation
图节点分类与消息传递 - 知乎
手写Koa.js源码