2014-07-05 2 views
0

if 문을 사용하여 android의 퀴즈 앱에 대한 답변을 증가시킵니다. 나는 루프 또는 while 루프가 berrter를 작동시켜야한다고 생각한다. 그러나 나는 또한 그것을 테스트했습니다. 지금 잘 받고 있습니다.안드로이드의 라디오 버튼에 대해 카운터가 증가하지 않음

아래 코드는 제 코드입니다. 고마워요

package newton.quizapplication; 

import android.app.Activity; 
import android.database.CursorJoiner; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 


public class MyActivity extends Activity { 
private RadioGroup radioGroup1; 
private RadioGroup radioGroup2; 
private RadioGroup radioGroup3; 
private RadioButton radioButton1; 
private RadioButton radioButton2; 
private RadioButton radioButton3; 
private Button btnSubmit; 
private int Ans = 0; 


private TextView Total; 

// private int wrong = 0;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    String All; 
    Total = (TextView)findViewById(R.id.Score); 


    addListenerOnButton(); 
} 

public void addListenerOnButton() { 
    radioGroup1 = (RadioGroup) findViewById(R.id.rg1); 
    radioGroup2 = (RadioGroup) findViewById(R.id.rg2); 
    radioGroup2 = (RadioGroup) findViewById(R.id.rg3); 
    btnSubmit = (Button) findViewById(R.id.submit); 

    btnSubmit.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      // get selected radio button from radioGroup 
      int selectedId1 = radioGroup1.getCheckedRadioButtonId(); 
      // find the radiobutton by returned id 
      radioButton1 = (RadioButton) findViewById(selectedId1); 

      // get selected radio button from radioGroup 
      int selectedId2 = radioGroup1.getCheckedRadioButtonId(); 
      // find the radiobutton by returned id 
      radioButton2 = (RadioButton) findViewById(selectedId2); 

      // get selected radio button from radioGroup 
      int selectedId3 = radioGroup1.getCheckedRadioButtonId(); 
      // find the radiobutton by returned id 
      radioButton3 = (RadioButton) findViewById(selectedId3); 


       if (radioButton1.getText().equals("Hyper Text Markup Language")) { 

        Ans++; 

       } 

        else if (radioButton2.getText().equals("The World Wide Web Consortium")) { 
         // Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show(); 
         Ans++; 

        } 
        else if (radioButton2.getText().equals("Specific places within a particular page")) { 

         Ans++; 

        } 


        Total.setText("You Got " + Ans + " Answer correct out of 3"); 


      Ans =0; 
     } 

    }); 

} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 
} 
+0

예상되는 동작은 무엇이며 무엇이 잘못된 동작입니까? – Eran

+0

그럼 정확히 "* 증가하지 않음 *"과 어떤 조건에서입니까? –

+0

나는 Total.setText가 3 중에서 대답을 설정하기를 원합니다. 오직 당신이 모든 답을 고르더라도 3 중에서 올바른 답을 얻었습니다. @Eran – Softwarex3

답변

0

당신은 당신의 조건을 변경해야합니다 : if (...) ... else if (...) ... else if (...) ...의 원래 코드에서

if (radioButton1.getText().equals("Hyper Text Markup Language")) { 
    Ans++; 
} 
if (radioButton2.getText().equals("The World Wide Web Consortium")) { 
    // Toast.makeText(MyActivity.this, "Incorrect Answer", Toast.LENGTH_SHORT).show(); 
    Ans++; 
} 
if (radioButton3.getText().equals("Specific places within a particular page")) { 
    Ans++; 
} 

는 단 하나의 조건이 조건이 true로 평가 처음부터 (충족시킬 수에게, 다른 조건은 없을 것입니다 확인). 따라서 Ans는 0 또는 1 만 가능합니다.

또한 마지막으로 radioButon3을 확인해야합니다.

+0

그 전에 시도해 보았습니다. 그것의 첫 번째 라디오 버튼을 찍은 것 같아요 그리고 다음 하나를 이동하지 않았다 .. 그리고 3 라디오 버튼을 정정 주셔서 감사합니다 .. 아직 아직 작동하지 않습니다 – Softwarex3

+0

@ Softwarex3'radioButton1 .getText(). equals ("하이퍼 텍스트 마크 업 언어")'그리고 당신은 if ... else if ... else if ...'코드를 가지고 있습니다. 구별 조건을 사용하면 코드에서 문제가 발생하지 않는 한 작동해야합니다. – Eran

+0

나도 몰라. 어쨌든 시간 내 주셔서 감사합니다. 윌은 계속 노력하고있어. – Softwarex3

관련 문제