<少女> 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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。