我有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);
.......................
这是我在活动中进行修改并尝试使用Hashtag(这是第二项活动)
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带有标签insinde
<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" />
..............
答案 0 :(得分:1)
您可以使用一个数组和一个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);
答案 1 :(得分:1)
一种解决方案是使用$result = [];
$ports = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$porstCount = count($ports);
for ($i = 0; $i < $portsCount; $i++) {
$result[] = $ports[$i];
if ($i > 0 && $i % 5 === 0) {
$result[] = 'new element';
}
}
// $result will be [1, 2, 3, 4, 5, 'new element', 6, 7, 8, 9, 10, 'new element', 11, 12]
及其相反版本
HashMap
致电
HashMap<CheckBox,Integer> map = new HashMap();
map.put(checkBox0,0);
map.put(checkBox1,1);
map.put(checkBox2,2);
map.put(checkBox3,3);
map.put(checkBox4,4);
map.put(checkBox5,5);
.
.
.
map.put(checkBox25,25);
Map<String, Character> reverseMap = new HashMap<>();
for(Map.Entry<Character, String> entry : map.entrySet()){
reverseMap.put(entry.getValue(), entry.getKey());
}
public void checkValue(CheckBox checkbox, boolean checkBoxValue)
{
if(checkBoxValue)
{
for(int i=map.get(checkbox); i>=0; i--)
reverseMap.get(i).setChecked(true);
}
else
checkbox.setChecked(false);
}
答案 2 :(得分:1)
尝试这种方式
在所有复选框中提供标签,而不是下面的ID
layout.xml
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="0"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"/>
.
.
.
.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="25"/>
Activity.java
使用Activity中的findViewWithTag查找所有CheckBox视图,并全部设置侦听器,并在侦听器事件中进行如下处理
CheckBox cbox;
cbox = (CheckBox) findViewWithTag("0");
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);
cb.setChecked(true);
}
}
}
});