<少女> 2022-11-16 16:03 采纳率: 90.9%
浏览 0
已结题

想要一点提交按钮,就把单选跟复选选中的结果返回输出出来,但是我这里的复选显示不出来,请问这里怎么把局部变量给全局变量,以至于后面我调用str是可以显示出来结果的?详解必采纳


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();
            }
        });
    }
}

img


就是我想要给这个复选框也进行监听设置,就是把选中的复选框返回给那个提交按钮,也就是想要一点提交按钮,就把单选跟复选选中的结果返回输出出来,但是我这里的复选显示不出来,请问这里怎么把局部变量给全局变量,以至于后面我调用str是可以显示出来结果的?详解必采纳

  • 写回答

2条回答 默认 最新

  • Plantago 2022-11-16 16:21
    关注

    你只监听了checkbox1的内容,其他两个也需要监听啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月25日
  • 已采纳回答 11月17日
  • 修改了问题 11月16日
  • 创建了问题 11月16日

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口