package com.example.studytest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox checkBox1,checkBox2,checkBox3;
RadioButton radioButton;
RadioGroup rg;
String str="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//对点选按钮进行监听,就只需要对RadioGroup进行监听
rg = (RadioGroup) findViewById(R.id.rg1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//对单选按钮监听的时候,对具体的RadioButton进行监听。这里可以不用对各个RadioButton的id进行监听,直接监听RadioGroup就可以
radioButton = (RadioButton) findViewById(checkedId);
// Toast.makeText(MainActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
checkBox1 = findViewById(R.id.checkbox1);
checkBox2 = findViewById(R.id.checkbox2);
checkBox2 = findViewById(R.id.checkbox3);
checkBox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox1.isChecked()){
str+=checkBox1.getText().toString();
// Toast.makeText(MainActivity.this,"str",Toast.LENGTH_SHORT).show();
}
}
});
Button button = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, radioButton.getText()+str,Toast.LENGTH_SHORT).show();
}
});
}
}
package com.example.studytest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox1,checkBox2,checkBox3;
private Button button;
private RadioButton radioButton;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//对点选按钮进行监听,就只需要对RadioGroup进行监听
rg = (RadioGroup) findViewById(R.id.rg1);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//对单选按钮监听的时候,对具体的RadioButton进行监听。这里可以不用对各个RadioButton的id进行监听,直接监听RadioGroup就可以
radioButton = (RadioButton) findViewById(checkedId);
Toast.makeText(MainActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
// checkBox1 = findViewById(R.id.checkbox1);
// checkBox2 = findViewById(R.id.checkbox2);
// checkBox2 = findViewById(R.id.checkbox3);
// checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if (checkBox1.isChecked()) {
// String str = "";
// str += checkBox1.getText().toString();
// Toast.makeText(MainActivity.this, checkBox1.getText(), Toast.LENGTH_SHORT).show();
// }
// }
// });
// checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if(checkBox2.isChecked()){
// String str="";
// str+=checkBox2.getText().toString();
// Toast.makeText(MainActivity.this,checkBox2.getText(),Toast.LENGTH_SHORT).show();
// }
// if(checkBox3.isChecked()){
// String str="";
// str+=checkBox3.getText().toString();
// Toast.makeText(MainActivity.this,checkBox3.getText(),Toast.LENGTH_SHORT).show();
// }
// }
// });
// checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// if(checkBox3.isChecked()){
// String str="";
// str+=checkBox2.getText().toString();
// Toast.makeText(MainActivity.this,checkBox3.getText(),Toast.LENGTH_SHORT).show();
// }
// if(checkBox3.isChecked()){
// String str="";
// str+=checkBox3.getText().toString();
// Toast.makeText(MainActivity.this,checkBox3.getText(),Toast.LENGTH_SHORT).show();
// }
// }
// });
button = (Button)findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="";
if(checkBox1.isChecked()){
str+=checkBox1.getText().toString();
// Toast.makeText(MainActivity.this,"str",Toast.LENGTH_SHORT).show();
}
if(checkBox2.isChecked()) {
str += checkBox2.getText().toString();
}
if(checkBox3.isChecked()) {
str += checkBox3.getText().toString();
}
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
}
就是我想要给这个复选框也进行监听设置,就是把选中的复选框返回给那个提交按钮,也就是想要一点提交按钮,就把单选跟复选选中的结果返回输出出来,但是我这里的复选显示不出来,请问这里怎么把局部变量给全局变量,以至于后面我调用str是可以显示出来结果的?详解必采纳