I have a activity which contains 3 different CheckBox. Now I want to validate that at least 2 CheckBox must be selected but I am not able to achieve this. Please help.
Code:
Boolean checkPC = chkProtectMoney.isChecked();
Boolean checkGM = chkGrow_my_money.isChecked();
Boolean checkMN = chkMedicalNeeds.isChecked();
if(!(checkPC && checkGM == true) || !(checkGM && checkMN == true) || !(checkPC && checkMN == true) || !(checkPC && checkGM && checkPC == true)){
Toast.makeText(getActivity(),R.string.two_expertise_caution,Toast.LENGTH_SHORT).show();
}
private boolean checkIfAnyTwoAreChecked() {
int sum = 0;
if(checkbox1.isChecked()){ sum++; }
if(checkbox2.isChecked()){ sum++; }
if(checkbox3.isChecked()){ sum++; }
return sum >= 2;
}
You can Use a int count .
private int getCheckedCount() {
int count = 0;
if (checkBox1.isChecked())
count++;
if (checkBox2.isChecked())
count++;
if (checkBox3.isChecked())
count++;
return count;
}
if(getCheckedCount()>=2){
// Do your stuff
}
OR
if(checkBox1.isChecked() && checkBox2.isChecked()
|| checkBox1.isChecked() && checkBox3.isChecked()
|| checkBox2.isChecked() && checkBox3.isChecked()){
// Do stuff
}
Add checkedChangeListener for all checkboxes and update count based on check as in
int checkedCount = 0;
chkProtectMoney.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
checkedCount++;
}else{
checkedCount--;
}
if(checkedCount >= 2){
// voila
}else{
//alert to check atleast two
}
}
});
//add for other two same logic then
Change your conditional statement to this
if((checkPC && checkGM) || (checkGM && checkMN) || (checkPC && checkMN)) {
// YOUR CODE
}
I create a script that displays the results depending on the selected options.
I need: A, B, C, D (0,1,-1)
int A = 0;
int B = 0;
int C = 0;
int D = 0;
Ansver will be one of 16 combination.
if A = 1 = A1;
if A = -1 = A2;
if B = 1 = B1;
if B = -1 B2;
if C = 1 = C1;
If C = -1 = C2;
If D = 1 = D1;
if D = -1 = D1;
A1;B1;C1;D1 = Nr1
A1;B1;C1;D2 = Nr2
A1;B1;C2;D1 = Nr3
A1;B1;C2;D2 = Nr4
A1;B2;C1;D1 = Nr5
A1;B2;C1;D2 = Nr6
A1;B2;C2;D1 = Nr7
A1;B2;C2;D2 = Nr8
A2;B1;C1;D1 = Nr9
A2;B1;C1;D2 = Nr10
A2;B1;C2;D1 = Nr11
A2;B1;C2;D2 = Nr12
A2;B2;C1;D1 = Nr13
A2;B2;C1;D2 = Nr14
A2;B2;C2;D1 = Nr15
A2;B2;C2;D2 = Nr16
I want to show the potential variants.
When you press the combination of: A1, B1 = "Nr1,Nr2,Nr3,Nr4"
When you press the combination of: A1, D1 = "Nr1,Nr3,Nr5,Nr7"
Later will be more variables E, F, G, H .... etc. But the answers will only 16th
What could be the logic, I'm a little stuck?
If / Else seems to be too long.
Example if/else:
if (A == 1) {
if (B == 1) {
if (C == 1) {
if (D == 1) {
scoreTeamA = "Nr1"; //A1,B1,C1,D1
} else if (D == -1) {
scoreTeamA = "Nr2"; //A1,B1,C1,D2
} else scoreTeamA = "Nr1, Nr2"; //A1,B1,C1
} else if (B == 1) {
if (D == 1) {
scoreTeamA = "Nr3"; //A1,B1,C1,D1
} else if (D == -1) {
scoreTeamA = "Nr4"; //A1,B1,C1,D2
} else scoreTeamA = "Nr3,Nr4"; //A1,B1,C2
} else scoreTeamA = "Nr1,Nr2,Nr3,Nr4"; //A1,B1
} else if (B == -1) {
scoreTeamA = "Nr5,Nr6,Nr7,Nr8";//A1,B2
} else
scoreTeamA = "Nr1,Nr2,Nr3,Nr4,Nr5,Nr6,Nr7,Nr8"; //A1
} else if (A == -1) {
scoreTeamA = "Nr9,Nr10,Nr11,Nr12,Nr13,Nr14,Nr15,Nr16"; //A2
}
Some of the alternatives that come to mind:
A long chain of if...else statements.
switch statements. This won't be much better than if...else, though.
A data structure, such as a Map.
A custom class hierarchy. Often a switch statement can be replaced by polymorphism. This is highly dependent on the specifics of what you are really doing.
For that you are using switch-case.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Click on red button 2 times and green button 3 times after this click on yellow button 5 times. If this done successfully you go on next screen for next quiz.
Assuming your buttons already connected to corresponding functions in Android Manifest
private int redClicks = 0;
private int greenClicks = 0;
private int yellowClicks = 0;
public void redClickCount(View view) {
if (redClicks < 2){
redClicks++;
}
greenClicks = 0;
yellowClicks = 0;
}
public void greenClickCount(View view) {
if (redClicks == 2 && greenClicks < 3){
greenClicks++;
} else { //start all over again
redClicks = 0;
greenClicks = 0;
yellowClicks = 0;
}
}
public void yellowClickCount(View view) {
if (redClicks == 2 && greenClicks == 3 && yellowClicks <5){
yellowClicks++;
} else {
if (redClicks == 2 && greenClicks == 3 && yellowClicks = 5){
// go to next round
} else {
//start all over again
redClicks = 0;
greenClicks = 0;
yellowClicks = 0;
}
}
}
I need to be able to display text with all words starting with a # in a different color and they should be clickable. How can i do this?
This should do the trick
private void setTags(TextView pTextView, String pTagString) {
SpannableString string = new SpannableString(pTagString);
int start = -1;
for (int i = 0; i < pTagString.length(); i++) {
if (pTagString.charAt(i) == '#') {
start = i;
} else if (pTagString.charAt(i) == ' ' || pTagString.charAt(i) == '\n' || (i == pTagString.length() - 1 && start != -1)) {
if (start != -1) {
if (i == pTagString.length() - 1) {
i++; // case for if hash is last word and there is no
// space after word
}
final String tag = pTagString.substring(start, i);
string.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Log.d("Hash", String.format("Clicked %s!", tag));
}
#Override
public void updateDrawState(TextPaint ds) {
// link color
ds.setColor(Color.parseColor("#33b5e5"));
ds.setUnderlineText(false);
}
}, start, i, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = -1;
}
}
}
pTextView.setMovementMethod(LinkMovementMethod.getInstance());
pTextView.setText(string);
}
I made a statement and if it is true it continues, I want to stop this "continue" and make another statement for example touchdown and touchup.
here is my code
private void updateRunning(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type != TouchEvent.TOUCH_UP)
continue;
world.doodle.DOODLE_Y = 3;
touchPoint.set(event.x, event.y);
guiCam.touchToWorld(touchPoint);
if (OverlapTester.pointInRectangle(pauseBounds, touchPoint)) {
Assets.clicks();
state = GAME_PAUSED;
return;
}
}
world.update(deltaTime, game.getInput().getAccelX());
if (world.score != lastScore) {
lastScore = world.score;
scoreString = "" + lastScore;
}
if (world.state == World.WORLD_STATE_NEXT_LEVEL) {
state = GAME_LEVEL_END;
}
if (world.state == World.WORLD_STATE_GAME_OVER) {
state = GAME_OVER;
if (lastScore >= Settings.highscores[4])
scoreString = "new highscore: " + lastScore;
else
scoreString = "score: " + lastScore;
Settings.addScore(lastScore);
Settings.save(game.getFileIO());
}
}
Little confused by what you are asking, but perhaps an else if?
if (event.type == TouchEvent.TOUCH_UP) {
/* do something for TOUCH_UP event */
} else if (event.type == TouchEvent.TOUCH_DOWN) {
/* do something for TOUCH_DOWN event */
} else {
/* do something else */
}
You can't stop a continue after you execute it.
Try adding break; where you want it to stop.