2017-11-30 6 views
1

チェックするチェックボックスの数の割合をロードするにはprogressBarが必要です。 progressBar with 5チェックボックス

Iは5チェックボックスのためにそれを使用しようとした:それが確認された場合

"progressbar.setProgress(20)"、それが "progressbar.setProgress(-20)"

プログレスなかった場合5つのチェックボックスがチェックされていても「20%」だけロードされます。

パブリッククラスMainActivityはあなたの問題は、あなたが常に代わりに現在の値から、それを更新する20または-20に設定されAppCompatActivity {

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


    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); 
    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2); 
    final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3); 
    final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4); 
    final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5); 


    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    final SharedPreferences.Editor editor = preferences.edit(); 
    if (preferences.contains("checkbox1") && preferences.getBoolean("checkbox1", false) == true) { 
     checkBox.setChecked(true); 
    } else { 
     checkBox.setChecked(false); 

    } 
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (checkBox.isChecked()) { 
       editor.putBoolean("checkbox1", true); 
       progressBar.setProgress(20); 
       editor.apply(); 
      } else { 
       editor.putBoolean("checkbox1", false); 
       progressBar.setProgress(-20); 
       editor.apply(); 
      } 
     } 
    }); 


    if (preferences.contains("checkbox2") && preferences.getBoolean("checkbox2", false) == true) { 
     checkBox2.setChecked(true); 
    } else { 
     checkBox2.setChecked(false); 

    } 
    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (checkBox2.isChecked()) { 
       editor.putBoolean("checkbox2", true); 
       progressBar.setProgress(20); 
       editor.apply(); 
      } else { 
       editor.putBoolean("checkbox2", false); 
       progressBar.setProgress(-20); 
       editor.apply(); 
      } 
     } 
    }); 


    if (preferences.contains("checkbox3") && preferences.getBoolean("checkbox3", false) == true) { 
     checkBox3.setChecked(true); 
    } else { 
     checkBox3.setChecked(false); 

    } 
    checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (checkBox3.isChecked()) { 
       editor.putBoolean("checkbox3", true); 
       progressBar.setProgress(20); 
       editor.apply(); 
      } else { 
       editor.putBoolean("checkbox3", false); 
       progressBar.setProgress(-20); 
       editor.apply(); 
      } 
     } 
    }); 

    if (preferences.contains("checkbox4") && preferences.getBoolean("checkbox4", false) == true) { 
     checkBox4.setChecked(true); 
    } else { 
     checkBox4.setChecked(false); 

    } 
    checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (checkBox4.isChecked()) { 
       editor.putBoolean("checkbox4", true); 
       progressBar.setProgress(20); 
       editor.apply(); 
      } else { 
       editor.putBoolean("checkbox4", false); 
       progressBar.setProgress(-20); 
       editor.apply(); 
      } 
     } 
    }); 

    if (preferences.contains("checkbox5") && preferences.getBoolean("checkbox5", false) == true) { 
     checkBox5.setChecked(true); 
    } else { 
     checkBox5.setChecked(false); 

    } 
    checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (checkBox5.isChecked()) { 
       editor.putBoolean("checkbox5", true); 
       progressBar.setProgress(20); 
       editor.apply(); 
      } else { 
       editor.putBoolean("checkbox5", false); 
       progressBar.setProgress(-20); 
       editor.apply(); 
      } 
     } 
    }); 

} 

}

+0

ようこそを使用!質問は明確かつ具体的であり、[最小で完全で検証可能な例](https://stackoverflow.com/help/mcve)が含まれていると、良い助けを受ける可能性が最も高いです。あなたが問題を抱えている実際のコードを投稿してください。 – bnaecker

+0

準備完了。ありがとう@bnaecker:D –

答えて

1

を拡張します。スタックオーバーフローに

progressBar.setProgress(progressBar.getProgress()+20); 

または

progressBar.setProgress(progressBar.getProgress()-20); 
+0

ああ、ありがとうございます非常に:D –