//设置提交按钮监听
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("我是方法一");
// 初始性别字符串
String Sex = "";
//循环单选组
for (int i = 0; i < radioButton.getChildCount(); i++) {
RadioButton r = (RadioButton) radioButton.getChildAt(i);
if (r.isChecked()) {
Sex = r.getText().toString();
// 先LogCat测试一下
// System.out.println(Sex);
break;
}
}
// 初始化爱好字符串
String userLove = "";
if (checkBox1.isChecked()) {
userLove += (String) checkBox1.getText().toString() + " ";
}
if (checkBox2.isChecked()) {
userLove += (String) checkBox2.getText().toString() + " ";
}
if (checkBox3.isChecked()) {
userLove += (String) checkBox3.getText().toString() + " ";
}
System.out.println(userLove);
// 广播
Toast.makeText(MainActivity.this,
"你填写的信息如下\n" + "用户名:" + userName.getText()
+ "\n密码:" + userPassword.getText()
+ "\n性别:" + Sex
+ "\n爱好:" + userLove,
Toast.LENGTH_LONG).show();
}
});
package com.zt.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
/**
* 2021.10.6
* 设置提交按钮监听,点击按钮后,广播出用户填写的信息
* Written by Zhong Tao
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
// 提交按钮
Button submitButton;
Button submitButton3;
// 用户名输入框
EditText userName;
// 用户密码输入框
EditText userPassword;
// 性别单选框
RadioGroup radioButton;
// 爱好多选框
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 2021.10.6
* 设置提交按钮监听,点击按钮后,广播出用户填写的信息
* Written by Zhong Tao
*/
//为对象赋值
userName = findViewById(R.id.userNameInput);
userPassword = findViewById(R.id.passwordInput);
radioButton = (RadioGroup) findViewById(R.id.sexGroup);
submitButton = findViewById(R.id.submit);
submitButton3 = findViewById(R.id.submit3);
//多选框数量
int checkBoxNum = 3;
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
//设置提交按钮监听(内部类方式)
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("我是方法一");
// 初始性别字符串
String Sex = "";
//循环单选组
for (int i = 0; i < radioButton.getChildCount(); i++) {
RadioButton r = (RadioButton) radioButton.getChildAt(i);
if (r.isChecked()) {
Sex = r.getText().toString();
// 先LogCat测试一下
// System.out.println(Sex);
break;
}
}
// 初始化爱好字符串
String userLove = "";
if (checkBox1.isChecked()) {
userLove += (String) checkBox1.getText().toString() + " ";
}
if (checkBox2.isChecked()) {
userLove += (String) checkBox2.getText().toString() + " ";
}
if (checkBox3.isChecked()) {
userLove += (String) checkBox3.getText().toString() + " ";
}
System.out.println(userLove);
// 广播
Toast.makeText(MainActivity.this,
"你填写的信息如下\n" + "用户名:" + userName.getText()
+ "\n密码:" + userPassword.getText()
+ "\n性别:" + Sex
+ "\n爱好:" + userLove,
Toast.LENGTH_LONG).show();
}
});
submitButton3.setOnClickListener(this);
}
//控件属性的事件监听方法
public void showInput(View view) {
System.out.println("我是方法二");
// 初始性别字符串
String Sex = "";
//循环单选组
for (int i = 0; i < radioButton.getChildCount(); i++) {
RadioButton r = (RadioButton) radioButton.getChildAt(i);
if (r.isChecked()) {
Sex = r.getText().toString();
// 先LogCat测试一下
// System.out.println(Sex);
break;
}
}
// 初始化爱好字符串
String userLove = "";
if (checkBox1.isChecked()) {
userLove += (String) checkBox1.getText().toString() + " ";
}
if (checkBox2.isChecked()) {
userLove += (String) checkBox2.getText().toString() + " ";
}
if (checkBox3.isChecked()) {
userLove += (String) checkBox3.getText().toString() + " ";
}
System.out.println(userLove);
// 广播
Toast.makeText(MainActivity.this,
"你填写的信息如下\n" + "用户名:" + userName.getText()
+ "\n密码:" + userPassword.getText()
+ "\n性别:" + Sex
+ "\n爱好:" + userLove,
Toast.LENGTH_LONG).show();
}
//通过事件源所在类实现
@Override
public void onClick(View v) {
System.out.println("我是方法三");
// 初始性别字符串
String Sex = "";
//循环单选组
for (int i = 0; i < radioButton.getChildCount(); i++) {
RadioButton r = (RadioButton) radioButton.getChildAt(i);
if (r.isChecked()) {
Sex = r.getText().toString();
// 先LogCat测试一下
// System.out.println(Sex);
break;
}
}
// 初始化爱好字符串
String userLove = "";
if (checkBox1.isChecked()) {
userLove += (String) checkBox1.getText().toString() + " ";
}
if (checkBox2.isChecked()) {
userLove += (String) checkBox2.getText().toString() + " ";
}
if (checkBox3.isChecked()) {
userLove += (String) checkBox3.getText().toString() + " ";
}
System.out.println(userLove);
// 广播
Toast.makeText(MainActivity.this,
"你填写的信息如下\n" + "用户名:" + userName.getText()
+ "\n密码:" + userPassword.getText()
+ "\n性别:" + Sex
+ "\n爱好:" + userLove,
Toast.LENGTH_LONG).show();
}
}