0

我正在制作一个包含 39 个复选框的应用程序。我希望它计算当前单击的复选框的百分比。这是应用程序的java代码:

public class Bifrost extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bifrost);
    //SPREMENLJIVKE
    final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
    cb1.setChecked(getFromSP("cb1"));
    cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
    cb2.setChecked(getFromSP("cb2"));
    cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
    cb3.setChecked(getFromSP("cb3"));
    cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
    cb4.setChecked(getFromSP("cb4"));
    cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb4",isChecked);
        }
    });
    final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
    cb5.setChecked(getFromSP("cb5"));
    cb5.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb5",isChecked);   
        }
    });
    final CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6);
    cb6.setChecked(getFromSP("cb6"));
    cb6.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb6",isChecked);   
        }
    });
    final CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7);
    cb7.setChecked(getFromSP("cb7"));
    cb7.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb7",isChecked);     
        }
    });
    final CheckBox cb8 = (CheckBox) findViewById(R.id.checkBox8);
    cb8.setChecked(getFromSP("cb8"));
    cb8.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9);
    cb9.setChecked(getFromSP("cb9"));
    cb9.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb9",isChecked);    
        }
    });
    final CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10);
    cb10.setChecked(getFromSP("cb10"));
    cb10.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb10",isChecked);    
        }
    });
    final CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11);
    cb11.setChecked(getFromSP("cb11"));
    cb11.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb11",isChecked);     
        }
    });
    final CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12);
    cb12.setChecked(getFromSP("cb12"));
    cb12.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb12",isChecked);
        }
    });
    final CheckBox cb13 = (CheckBox) findViewById(R.id.checkBox13);
    cb13.setChecked(getFromSP("cb13"));
    cb13.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb13",isChecked);    
        }
    });
    final CheckBox cb14 = (CheckBox) findViewById(R.id.checkBox14);
    cb14.setChecked(getFromSP("cb14"));
    cb14.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb15 = (CheckBox) findViewById(R.id.checkBox15);
    cb15.setChecked(getFromSP("cb15"));
    cb15.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb15",isChecked);   
        }
    });
    final CheckBox cb16 = (CheckBox) findViewById(R.id.checkBox16);
    cb16.setChecked(getFromSP("cb16"));
    cb16.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb16",isChecked);      
        }
    });
    final CheckBox cb17 = (CheckBox) findViewById(R.id.checkBox17);
    cb17.setChecked(getFromSP("cb17"));
    cb17.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb17",isChecked);   
        }
    });
    final CheckBox cb18 = (CheckBox) findViewById(R.id.checkBox18);
    cb18.setChecked(getFromSP("cb18"));
    cb18.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb18",isChecked);     
        }
    });
    final CheckBox cb19 = (CheckBox) findViewById(R.id.checkBox19);
    cb19.setChecked(getFromSP("cb19"));
    cb19.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb20 = (CheckBox) findViewById(R.id.checkBox20);
    cb20.setChecked(getFromSP("cb20"));
    cb20.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb20",isChecked);   
        }
    });
    final CheckBox cb21 = (CheckBox) findViewById(R.id.checkBox21);
    cb21.setChecked(getFromSP("cb21"));
    cb21.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb22 = (CheckBox) findViewById(R.id.checkBox22);
    cb22.setChecked(getFromSP("cb22"));
    cb22.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb23 = (CheckBox) findViewById(R.id.checkBox23);
    cb23.setChecked(getFromSP("cb23"));
    cb23.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb24 = (CheckBox) findViewById(R.id.checkBox24);
    cb24.setChecked(getFromSP("cb24"));
    cb24.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb25 = (CheckBox) findViewById(R.id.checkBox25);
    cb25.setChecked(getFromSP("cb25"));
    cb25.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb25",isChecked);      
        }
    });
    final CheckBox cb26 = (CheckBox) findViewById(R.id.checkBox26);
    cb26.setChecked(getFromSP("cb26"));
    cb26.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb26",isChecked);     
        }
    });
    final CheckBox cb27 = (CheckBox) findViewById(R.id.checkBox27);
    cb27.setChecked(getFromSP("cb27"));
    cb27.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb27",isChecked);     
        }
    });
    final CheckBox cb28 = (CheckBox) findViewById(R.id.checkBox28);
    cb28.setChecked(getFromSP("cb28"));
    cb28.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb28",isChecked);     
        }
    });
    final CheckBox cb29 = (CheckBox) findViewById(R.id.checkBox29);
    cb29.setChecked(getFromSP("cb29"));
    cb29.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb29",isChecked);     
        }
    });
    final CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30);
    cb30.setChecked(getFromSP("cb30"));
    cb30.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb30",isChecked);      
        }
    });
    final CheckBox cb31 = (CheckBox) findViewById(R.id.checkBox31);
    cb31.setChecked(getFromSP("cb31"));
    cb31.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb31",isChecked);     
        }
    });
    final CheckBox cb32 = (CheckBox) findViewById(R.id.checkBox32);
    cb32.setChecked(getFromSP("cb32"));
    cb32.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        }
    });
    final CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33);
    cb33.setChecked(getFromSP("cb33"));
    cb33.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb33",isChecked);      
        }
    });
    final CheckBox cb34 = (CheckBox) findViewById(R.id.checkBox34);
    cb34.setChecked(getFromSP("cb34"));
    cb34.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb34",isChecked);    
        }
    });
    final CheckBox cb35 = (CheckBox) findViewById(R.id.checkBox35);
    cb35.setChecked(getFromSP("cb35"));
    cb35.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb35",isChecked);      
        }
    });
    final CheckBox cb36 = (CheckBox) findViewById(R.id.checkBox36);
    cb36.setChecked(getFromSP("cb36"));
    cb36.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb36",isChecked);      
        }
    });
    final CheckBox cb37 = (CheckBox) findViewById(R.id.checkBox37);
    cb37.setChecked(getFromSP("cb37"));
    cb37.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb37",isChecked);      
        }
    });
    final CheckBox cb38 = (CheckBox) findViewById(R.id.checkBox38);
    cb38.setChecked(getFromSP("cb38"));
    cb38.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb38",isChecked);    
        }
    });
    final CheckBox cb39 = (CheckBox) findViewById(R.id.checkBox39);
    cb39.setChecked(getFromSP("cb39"));
    cb39.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            saveInSp("cb39",isChecked);      
        }
    });


    //IF STAVKI ZA CHECKBOXE
    cb3.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb3",isChecked);
            if ( cb3.isChecked() )
            {
                cb4.setChecked(true);
                cb5.setChecked(true);
                cb6.setChecked(true);
                cb7.setChecked(true);
            }

        }
    });

    cb8.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb8",isChecked);
            if ( cb8.isChecked() )
            {
                cb9.setChecked(true);
                cb10.setChecked(true);
                cb11.setChecked(true);
                cb12.setChecked(true);
            }

        }
    });

    cb14.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb14",isChecked);
            if ( cb14.isChecked() )
            {
                cb15.setChecked(true);
                cb16.setChecked(true);
                cb17.setChecked(true);
                cb18.setChecked(true);
            }

        }
    });

    cb21.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb21",isChecked);
            if ( cb21.isChecked() )
            {
                cb25.setChecked(true);
            }

        }
    });

    cb24.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb24",isChecked);
            if ( cb24.isChecked() )
            {
                cb28.setChecked(true);
            }

        }
    });

    cb23.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb23",isChecked);
            if ( cb23.isChecked() )
            {
                cb24.setChecked(true);
                cb29.setChecked(true);
                cb30.setChecked(true);
                cb31.setChecked(true);
            }

        }
    });

    cb32.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb32",isChecked);
            if ( cb32.isChecked() )
            {
                cb33.setChecked(true);
                cb34.setChecked(true);
                cb35.setChecked(true);
                cb36.setChecked(true);
            }

        }
    });

    cb2.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb2",isChecked);
            if ( cb2.isChecked() )
            {
                cb3.setChecked(true);
                cb8.setChecked(true);
                cb13.setChecked(true);
                cb14.setChecked(true);
            }

        }
    });

    cb19.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb19",isChecked);
            if ( cb19.isChecked() )
            {
                cb20.setChecked(true);
                cb21.setChecked(true);
                cb26.setChecked(true);
                cb27.setChecked(true);
            }

        }
    });

    cb22.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb22",isChecked);
            if ( cb22.isChecked() )
            {
                cb23.setChecked(true);
                cb32.setChecked(true);
                cb37.setChecked(true);
                cb38.setChecked(true);
            }

        }
    });

    cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            saveInSp("cb1",isChecked);
            if ( cb1.isChecked() )
            {
                cb2.setChecked(true);
                cb19.setChecked(true);
                cb22.setChecked(true);
                cb39.setChecked(true);
            }

        }
    });
    }
}


//SP METODE ETC.
private boolean getFromSP(String key){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
    }
    private void saveInSp(String key,boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("GW2Legendary", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
    }

//BUTTON UNCHECK EVERYTHING   
    public void onClick (View v){

        CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
        CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
        CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
        CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
        CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
        CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6);
        CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7);
        CheckBox cb8 = (CheckBox) findViewById(R.id.checkBox8);
        CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9);
        CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10);
        CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11);
        CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12);
        CheckBox cb13 = (CheckBox) findViewById(R.id.checkBox13);
        CheckBox cb14 = (CheckBox) findViewById(R.id.checkBox14);
        CheckBox cb15 = (CheckBox) findViewById(R.id.checkBox15);
        CheckBox cb16 = (CheckBox) findViewById(R.id.checkBox16);
        CheckBox cb17 = (CheckBox) findViewById(R.id.checkBox17);
        CheckBox cb18 = (CheckBox) findViewById(R.id.checkBox18);
        CheckBox cb19 = (CheckBox) findViewById(R.id.checkBox19);
        CheckBox cb20 = (CheckBox) findViewById(R.id.checkBox20);
        CheckBox cb21 = (CheckBox) findViewById(R.id.checkBox21);
        CheckBox cb22 = (CheckBox) findViewById(R.id.checkBox22);
        CheckBox cb23 = (CheckBox) findViewById(R.id.checkBox23);
        CheckBox cb24 = (CheckBox) findViewById(R.id.checkBox24);
        CheckBox cb25 = (CheckBox) findViewById(R.id.checkBox25);
        CheckBox cb26 = (CheckBox) findViewById(R.id.checkBox26);
        CheckBox cb27 = (CheckBox) findViewById(R.id.checkBox27);
        CheckBox cb28 = (CheckBox) findViewById(R.id.checkBox28);
        CheckBox cb29 = (CheckBox) findViewById(R.id.checkBox29);
        CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30);
        CheckBox cb31 = (CheckBox) findViewById(R.id.checkBox31);
        CheckBox cb32 = (CheckBox) findViewById(R.id.checkBox32);
        CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33);
        CheckBox cb34 = (CheckBox) findViewById(R.id.checkBox34);
        CheckBox cb35 = (CheckBox) findViewById(R.id.checkBox35);
        CheckBox cb36 = (CheckBox) findViewById(R.id.checkBox36);
        CheckBox cb37 = (CheckBox) findViewById(R.id.checkBox37);
        CheckBox cb38 = (CheckBox) findViewById(R.id.checkBox38);
        CheckBox cb39 = (CheckBox) findViewById(R.id.checkBox39);

        switch (v.getId()) {
        case R.id.button1:
            cb1.setChecked(false);
            cb2.setChecked(false);
            cb3.setChecked(false);
            cb4.setChecked(false);
            cb5.setChecked(false);
            cb6.setChecked(false);
            cb7.setChecked(false);
            cb8.setChecked(false);
            cb9.setChecked(false);
            cb10.setChecked(false);
            cb11.setChecked(false);
            cb12.setChecked(false);
            cb13.setChecked(false);
            cb14.setChecked(false);
            cb15.setChecked(false);
            cb16.setChecked(false);
            cb17.setChecked(false);
            cb18.setChecked(false);
            cb19.setChecked(false);
            cb20.setChecked(false);
            cb21.setChecked(false);
            cb22.setChecked(false);
            cb23.setChecked(false);
            cb24.setChecked(false);
            cb25.setChecked(false);
            cb26.setChecked(false);
            cb27.setChecked(false);
            cb28.setChecked(false);
            cb29.setChecked(false);
            cb30.setChecked(false);
            cb31.setChecked(false);
            cb32.setChecked(false);
            cb33.setChecked(false);
            cb34.setChecked(false);
            cb35.setChecked(false);
            cb36.setChecked(false);
            cb37.setChecked(false);
            cb38.setChecked(false);
            cb39.setChecked(false);
        }
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bifrost, menu);
    return true;
}

}

我在想我可以通过实现一个计数器来实现这一点,这样每次检查复选框时都会执行 counter += 1 并且每次未选中复选框时都会执行 counter -= 1。之后我只需输入百分比 = ( 100 * counter) / 39. 但是由于我对 android 开发和 java 完全陌生,我对此有疑问。我应该在哪里初始化变量计数器,然后我应该在哪里插入 counter+=1 等。

我非常感谢任何形式的帮助/解释。

4

3 回答 3

0

您可以将 int counter 设为全局变量,然后将 actionlisteners 添加到每个复选框,以便根据复选框是否被选中/取消选中来添加或减去 counter。

于 2013-06-12T16:01:50.913 回答
0

将您的复选框添加到集合(或简单数组)中。遍历它,检查每一个看它是否被点击。计算那些被点击的并执行你的计算。

于 2013-06-12T16:00:52.950 回答
0

为了节省一些 LOC,您可以这样做:在您的类中创建一个新方法,它将检查每个框的状态:

public static void percentageChecked(){
Vector<CheckBox> allBoxes=new Vector<CheckBox>();
CheckBox cb1,cb2, cb3;...//declare all 39 checkboxes
int counterChecked=0;

cb1=(CheckBox)findViewById(R.id.checkBox1);
cb2=(CheckBox)findViewById(R.id.checkBox2);//initialize each checkbox from the layout
...
allBoxes.add(cb1);//add each checkbox to vector
...

//iterate through all checkboxes:

for(int i=0; i<allBoxes.size();i++)
   if(allBoxes.get(i).isChecked()){
   counterChecked++;
   }
}

float percent = (counterChecked * 100.0f) / allBoxes.size();
craftedPercent.setText(percent);
}

然后对于每个检查按钮覆盖 OnCheckedChangeListener:

cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        percentageChecked();
        }
 });
于 2013-06-12T16:11:28.260 回答