<少女> 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的内容,其他两个也需要监听啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • <少女> 2022-11-16 17:31
    关注
    
    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();
                }
            });
        }
    }
    
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 win10商店接入问题
  • ¥15 java 这种树形框吗
  • ¥40 找同学帮敲Python代码
  • ¥15 MYSQL 订单的商品明细重复计算问题
  • ¥15 微信实时共享位置修改
  • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
  • ¥50 共模反馈回路的小信号增益
  • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
  • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall
  • ¥15 诊断性META分析合并效能的检验