Java 如何自动选中下面的复选框?

标签 java android android-checkbox

我有 25 个复选框。我不想检查层次结构。如果我检查 checkbox2,它应该检查 checkbox2 和 checkbox1。如果我检查 checkbox3,它应该检查 checkbox3,2 和 1....

我已经有一个工作代码了。但 25 个复选框就太长了。我怎样才能使这段代码更小?

        if (checkBox0Value) {
        checkBox0.setChecked(true);
    } else {
        checkBox0.setChecked(false);
    }
    if (checkBox1Value) {
        checkBox1.setChecked(true);
        checkBox0.setChecked(true);
    } else {
        checkBox1.setChecked(false);
    }
    if (checkBox2Value) {
        checkBox2.setChecked(true);
        checkBox1.setChecked(true);
        checkBox0.setChecked(true);

      .......................

这是我在 Activity 中的编辑并尝试使用主题标签(这是第二个 Activity )

package com.example.customlist;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;

public class heroview extends Activity implements OnClickListener {


CheckBox cbox;
CheckBox checkBox0, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7,
         checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14,
         checkBox15, checkBox16, checkBox17, checkBox18, checkBox19, checkBox20, checkBox21,
         checkBox22, checkBox23, checkBox24;
// EditText editText, editText2, editText3, editText4, editText5, editText6;
Button button;
// Toolbar mToolbar;
// ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    cbox = (CheckBox) findViewWithTag("0"); //Cannot resolve Method findViewWithTag(java.lang.String) <-------------------------------
    cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                int loopValue = (int) buttonView.getTag();
                for(int i=0;i<loopValue;i++){
                    CheckBox cb = (CheckBox) findViewWithTag(i);//Cannot resolve Method findViewWithTag(int) <-------------------------------
                    cb.setChecked(true);
                }
            }
        }
    });

    //mToolbar = (Toolbar) findViewById(R.id.toolbar2);
    //mImageView = (ImageView) findViewById(R.id.ImageView2);
    checkBox0 = (CheckBox) findViewById(R.id.Lcheck0); //checkbox start
    checkBox1 = (CheckBox) findViewById(R.id.Lcheck1);
    checkBox2 = (CheckBox) findViewById(R.id.Lcheck2);
    ///checkBox3 = (CheckBox) findViewById(R.id.Lcheck3);
    //        checkBox4 = (CheckBox) findViewById(R.id.Lcheck4);
    //        checkBox5 = (CheckBox) findViewById(R.id.Lcheck5);
    //        checkBox6 = (CheckBox) findViewById(R.id.Lcheck6);
    //        checkBox7 = (CheckBox) findViewById(R.id.Lcheck7);
    //        checkBox8 = (CheckBox) findViewById(R.id.Lcheck8);
    //        checkBox9 = (CheckBox) findViewById(R.id.Lcheck9);
    //        checkBox10 = (CheckBox) findViewById(R.id.Lcheck10);
    //        checkBox11 = (CheckBox) findViewById(R.id.Lcheck11);
    //        checkBox12 = (CheckBox) findViewById(R.id.Lcheck12);
    //        checkBox13 = (CheckBox) findViewById(R.id.Lcheck13);
    //        checkBox14 = (CheckBox) findViewById(R.id.Lcheck14);
    //        checkBox15 = (CheckBox) findViewById(R.id.Lcheck15);
    //        checkBox16 = (CheckBox) findViewById(R.id.Lcheck16);
    //        checkBox17 = (CheckBox) findViewById(R.id.Lcheck17);
    //        checkBox18 = (CheckBox) findViewById(R.id.Lcheck18);
    //        checkBox19 = (CheckBox) findViewById(R.id.Lcheck19);
    //        checkBox20 = (CheckBox) findViewById(R.id.Lcheck20);
    //        checkBox21 = (CheckBox) findViewById(R.id.Lcheck21);
    //        checkBox22 = (CheckBox) findViewById(R.id.Lcheck22);
    //        checkBox23 = (CheckBox) findViewById(R.id.Lcheck23);
    //        checkBox24 = (CheckBox) findViewById(R.id.Lcheck24);
    //editText = (EditText) findViewById(R.id.edt1);
    //editText2 = (EditText) findViewById(R.id.edt2);
    //editText3 = (EditText) findViewById(R.id.edt3);
    //editText4 = (EditText) findViewById(R.id.edt4);
    //editText5 = (EditText) findViewById(R.id.edt5);
    //editText6 = (EditText) findViewById(R.id.edt6);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
    loadSavedPreferences();

    //Bundle mBundle = getIntent().getExtras();
    //        if (mBundle != null) {
    //            mToolbar.setTitle(mBundle.getString("heroName"));
    //            mImageView.setImageResource(mBundle.getInt("heroIcon"));
    //        }

}



private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    boolean checkBox0Value = sharedPreferences.getBoolean("CheckBox0_Value", false);
    boolean checkBox1Value = sharedPreferences.getBoolean("CheckBox1_Value", false);
    boolean checkBox2Value = sharedPreferences.getBoolean("CheckBox2_Value", false);
    ///boolean checkBox3Value = sharedPreferences.getBoolean("CheckBox3_Value", false);
    //        boolean checkBox4Value = sharedPreferences.getBoolean("CheckBox4_Value", false);
    //        boolean checkBox5Value = sharedPreferences.getBoolean("CheckBox5_Value", false);
    //        boolean checkBox6Value = sharedPreferences.getBoolean("CheckBox6_Value", false);
    //        boolean checkBox7Value = sharedPreferences.getBoolean("CheckBox7_Value", false);
    //        boolean checkBox8Value = sharedPreferences.getBoolean("CheckBox8_Value", false);
    //        boolean checkBox9Value = sharedPreferences.getBoolean("CheckBox9_Value", false);
    //        boolean checkBox10Value = sharedPreferences.getBoolean("CheckBox10_Value", false);
    //        boolean checkBox11Value = sharedPreferences.getBoolean("CheckBox11_Value", false);
    //        boolean checkBox12Value = sharedPreferences.getBoolean("CheckBox12_Value", false);
    //        boolean checkBox13Value = sharedPreferences.getBoolean("CheckBox13_Value", false);
    //        boolean checkBox14Value = sharedPreferences.getBoolean("CheckBox14_Value", false);
    //        boolean checkBox15Value = sharedPreferences.getBoolean("CheckBox15_Value", false);
    //        boolean checkBox16Value = sharedPreferences.getBoolean("CheckBox16_Value", false);
    //        boolean checkBox17Value = sharedPreferences.getBoolean("CheckBox17_Value", false);
    //        boolean checkBox18Value = sharedPreferences.getBoolean("CheckBox18_Value", false);
    //        boolean checkBox19Value = sharedPreferences.getBoolean("CheckBox19_Value", false);
    //        boolean checkBox20Value = sharedPreferences.getBoolean("CheckBox20_Value", false);
    //        boolean checkBox21Value = sharedPreferences.getBoolean("CheckBox21_Value", false);
    //        boolean checkBox22Value = sharedPreferences.getBoolean("CheckBox22_Value", false);
    //        boolean checkBox23Value = sharedPreferences.getBoolean("CheckBox23_Value", false);
    //        boolean checkBox24Value = sharedPreferences.getBoolean("CheckBox24_Value", false);
    //String textA = sharedPreferences.getString("TextA", "0");
    //String textB = sharedPreferences.getString("TextB", "0");
    //String textC = sharedPreferences.getString("TextC", "0");
    //String textD = sharedPreferences.getString("TextD", "0");
    //String textE = sharedPreferences.getString("TextE", "0");
    //String textF = sharedPreferences.getString("TextF", "0");


    //.......
    //.......
    //.......


    //editText.setText(textA);
    //editText2.setText(textB);
    //editText3.setText(textC);
    //editText4.setText(textD);
    //editText5.setText(textE);
    //editText6.setText(textF);
}

private void savePreferences(String key, boolean value) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

private void savePreferences(String key, String value) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

public void onClick(View v) {
    savePreferences("CheckBox0_Value", checkBox0.isChecked());
    savePreferences("CheckBox1_Value", checkBox1.isChecked());
    savePreferences("CheckBox2_Value", checkBox2.isChecked());
    ///savePreferences("CheckBox3_Value", checkBox3.isChecked());
    //        savePreferences("CheckBox4_Value", checkBox4.isChecked());
    //        savePreferences("CheckBox5_Value", checkBox5.isChecked());
    //        savePreferences("CheckBox6_Value", checkBox6.isChecked());
    //        savePreferences("CheckBox7_Value", checkBox7.isChecked());
    //        savePreferences("CheckBox8_Value", checkBox8.isChecked());
    //        savePreferences("CheckBox9_Value", checkBox9.isChecked());
    //        savePreferences("CheckBox10_Value", checkBox10.isChecked());
    //        savePreferences("CheckBox11_Value", checkBox11.isChecked());
    //        savePreferences("CheckBox12_Value", checkBox12.isChecked());
    //        savePreferences("CheckBox13_Value", checkBox13.isChecked());
    //        savePreferences("CheckBox14_Value", checkBox14.isChecked());
    //        savePreferences("CheckBox15_Value", checkBox15.isChecked());
    //        savePreferences("CheckBox16_Value", checkBox16.isChecked());
    //        savePreferences("CheckBox17_Value", checkBox17.isChecked());
    //        savePreferences("CheckBox18_Value", checkBox18.isChecked());
    //        savePreferences("CheckBox19_Value", checkBox19.isChecked());
    //        savePreferences("CheckBox20_Value", checkBox20.isChecked());
    //        savePreferences("CheckBox21_Value", checkBox21.isChecked());
    //        savePreferences("CheckBox22_Value", checkBox22.isChecked());
    //        savePreferences("CheckBox23_Value", checkBox23.isChecked());
    //        savePreferences("CheckBox24_Value", checkBox24.isChecked());

    //savePreferences("TextA", editText.getText().toString());
    //savePreferences("TextB", editText2.getText().toString());
    //savePreferences("TextC", editText3.getText().toString());
    //savePreferences("TextD", editText4.getText().toString());
    //savePreferences("TextE", editText5.getText().toString());
    //savePreferences("TextF", editText6.getText().toString());

    finish();
}
}

我的 XML 中有标签

<TableRow
            android:id="@+id/tablelvl0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center">

            <CheckBox
                android:id="@+id/Lcheck0"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed"
                android:gravity="center"
                android:tag="0"
                android:text="0"
                android:textColor="@color/colorAccent"
                android:textSize="14sp"
                android:textStyle="bold" />
                ..............

最佳答案

您可以使用数组和 id

public void checkUnCheck(boolean checkBoxValue,int checkBoxId){
    if(checkBoxValue) {
        for (int i = 0; i <= checkBoxId; i++) {
            myCheckBoxs[i].setChecked(true);
        }
    }else{
        myCheckBoxs[checkBoxId].setChecked(false);
    }
}

并调用

checkUnCheck(checkBox0Value,0);
checkUnCheck(checkBox1Value,1);
checkUnCheck(checkBox2Value,2);

关于Java 如何自动选中下面的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276265/

相关文章:

java - 加密不适用于从 openssl 生成的 AES-256 key

java - 使用 "visited"集进行递归优化,用于纯函数

android - 在 AppCompat 主题中覆盖默认 ListView 突出显示的颜色

java - 单击按钮时如何将多个复选框值发送到 Firebase 数据库 Android

Java OutputStream 到多个文件

java - 如何在 Spring MVC 中将未编码的字符串写入 View ?

android - 简单隐藏/混淆 APK 中的字符串?

android - 如何修复按钮对齐以在不同屏幕上具有相同的外观

java - 单击 ListView 时如何启用/禁用 ListView 中的复选框?

java - 如何从复选框中获取多个选中的值并防止输出显示两次