当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 使用CopyMemory API出现 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
- 商品管理系统——整合仓库服务以及获取仓库列表
- Chrome browser engine blink & V8
- Mapstructure detoxifies object mapping
- python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
- 2 普通模式
- libssl对CentOS登录的影响
- 百亿级数据分表后怎么分页查询?
- Talk about my understanding of FAAS with Alibaba cloud FC
- Five indispensable open source tools for me
猜你喜欢
JT-day09
卧槽,这年轻人不讲武德,应届生凭“小抄”干掉5年老鸟,成功拿到字节20Koffer
Show profile analysis of SQL statement performance overhead
An attempt to read or write to protected memory occurred using the CopyMemory API. This usually indicates that other memory is corrupted.
捕获冒泡?难道浏览器是鱼吗?
python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
Recommended tools for Mac
Understanding task and async await
EasyNTS上云网关设备在雪亮工程项目中的实战应用
《内网安全攻防》配套视频 之 利用PS查询域内信息
随机推荐
AI fresh student's annual salary has increased to 400000, you can still make a career change now!
JT-day09
《内网安全攻防》配套视频 之 利用PS查询域内信息
彩虹排序 | 荷兰旗问题
python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
Commodity management system -- the search function of SPU
How to do thread dump analysis in Windows Environment
Using stream to read and write files to process large files
Application of cloud gateway equipment on easynts in Xueliang project
SQL语句实现水仙花数求取
JT-day09
再读《重构》
From the practice, this paper discusses the problems caused by the inconsistent design of ruby syntax.
卧槽,这年轻人不讲武德,应届生凭“小抄”干掉5年老鸟,成功拿到字节20Koffer
Capture bubbles? Is browser a fish?
for与for...in、for Each和map和for of
Principle analysis and performance tuning of elasticsearch
Looking for better dynamic getter and setter solutions
MapStruct 解了对象映射的毒
[Python from zero to one] 5. Detailed explanation of beautiful soup basic syntax of web crawler