0

私はアンドロイドが初めてです..

グループのラジオボタンのチェックを外す際に問題に直面しています....

私の問題は....

私はクイズベースのアプリを開発しています。ユーザーがこのアプリのラジオ ボタンを開くと、1 つのクエストと 4 つのオプション (ラジオ ボタン) が表示されますが、ユーザーが 1 つのクエストに回答し、次のクエストのラジオ ボタンに進むと、問題が発生します。チェックを外したい/すべてのクエストのラジオ ボタンをリセットするにはどうすればよいですか?

私が直面しているもう1つの問題は...ユーザーが最初のボタンを選択し、再びオプションを変更したい場合、別のボタンを選択して選択できないはずです..

これを実装する方法は?

どんな助けでも大歓迎です。

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
     ques1=new ArrayList<String>(new ArrayList<String>(ques1));
        //  j=0;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j));
answ1=new ArrayList<String>(new ArrayList<String>(answ1));
        btn_practice1.setText(answ1.get(0));
        btn_practice2.setText(answ1.get(1));
        btn_practice3.setText(answ1.get(2));
        btn_practice4.setText(answ1.get(3));
        btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                String temp = radioButton.getText().toString();
                crrtans=new ArrayList<String>(new ArrayList<String>(crrtans));
                if (temp.equals(crrtans.get(l))){
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
                txtRadio.setTextColor(Color.parseColor("#008000"));
            }else{
                //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                   // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText() + "is INCORRECT");
                    txtRadio.setTextColor(Color.parseColor("#FF0000"));
            }
            }
     });
    Button nextBtn = (Button) findViewById(R.id.nxt_btn);
    nextBtn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){  
          if (j == ques1.size() -1) {
                finish();
            }
          else{ 
            ++j;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText(ques1.get(j));
            ++k;
            btn_practice1.setText(answ1.get((k*4)+0));
            btn_practice2.setText(answ1.get((k*4)+1));
            btn_practice3.setText(answ1.get((k*4)+2));
            btn_practice4.setText(answ1.get((k*4)+3));
          }
          btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    String temp = radioButton.getText().toString();
                    l++;
                    if (temp.equals(crrtans.get(l))){
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
                    txtRadio.setTextColor(Color.parseColor("#008000"));
                }else{
                    //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                       // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                        TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                        txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                        txtRadio.setTextColor(Color.parseColor("#FF0000"));
                }
                }      
         });
     }
});   
    Button previousbtn = (Button) findViewById(R.id.prv_btn);
    previousbtn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View v){
        if (j <= 0) {
            Toast.makeText(Question.this, "First Question",Toast.LENGTH_SHORT).show();
            } else {
            --j;
            TextView txtque = (TextView) findViewById(R.id.que_txt); 
            txtque.setText(ques1.get(j));
            --k;
            btn_practice1.setText(answ1.get((k*4)+0));
            btn_practice2.setText(answ1.get((k*4)+1));
            btn_practice3.setText(answ1.get((k*4)+2));
            btn_practice4.setText(answ1.get((k*4)+3));
            }
         btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) { 
                    RadioButton radioButton = (RadioButton) findViewById(checkedId);
                    String temp = radioButton.getText().toString();
                    l--;
                    if (temp.equals(crrtans.get(l))){
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
                    txtRadio.setTextColor(Color.parseColor("#008000"));
                }else{
                    //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                       // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                        TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                        txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                        txtRadio.setTextColor(Color.parseColor("#FF0000"));
                }
                }      
         });
         }
    });
    }

丸太猫

             E/AndroidRuntime(1457): FATAL EXCEPTION: main
             E/AndroidRuntime(1457): java.lang.NullPointerException
             E/AndroidRuntime(1457):    at 
           com.example.finalpractice.Question$LoadQuestions$1.onCheckedChanged(Question.java:205)
             E/AndroidRuntime(1457):    at 
           android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
             E/AndroidRuntime(1457):    at 
           android.widget.RadioGroup.check(RadioGroup.java:166)
             E/AndroidRuntime(1457):    at    
           android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
             E/AndroidRuntime(1457):    at 
           com.example.finalpractice.Question$LoadQuestions$2.onClick(Question.java:238)
             E/AndroidRuntime(1457):    at android.view.View.performClick(View.java:2485)
             E/AndroidRuntime(1457):    at android.view.View$PerformClick.run(View.java:9080)
             E/AndroidRuntime(1457):    at 
           android.os.Handler.handleCallback(Handler.java:587)
             E/AndroidRuntime(1457):    at 
           android.os.Handler.dispatchMessage(Handler.java:92)
4

3 に答える 3

4

これを使って..

RadioGroup radiogrp = (RadioGroup)findViewById(R.id.radiogrp);

オンクリックの内部

radiogrp.clearCheck();
于 2013-02-04T07:55:27.723 に答える
1

radioGroup.clearCheck();次のボタンに追加します。

 Button nextBtn = (Button) findViewById(R.id.nxt_btn);
nextBtn.setOnClickListener(new Button.OnClickListener(){
  public void onClick(View v){  
      if (j == ques1.size() -1) {
            finish();
        }
      else{ 
        ++j;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j));
        ++k;
        btn_practicerg.clearCheck();
        btn_practice1.setText(answ1.get((k*4)+0));
        btn_practice2.setText(answ1.get((k*4)+1));
        btn_practice3.setText(answ1.get((k*4)+2));
        btn_practice4.setText(answ1.get((k*4)+3));
      }
于 2013-02-04T07:58:14.890 に答える
0

これが解決策です。

onCheckedChanged() に次の 2 行のコードを追加します。

int selectedId = group.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) group.findViewById(selectedId);

コードに同じ変更を加えました。

Button previousbtn = (Button) findViewById(R.id.prv_btn);
previousbtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
    if (j <= 0) {
        Toast.makeText(Question.this, "First Question",Toast.LENGTH_SHORT).show();
        } else {
        --j;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j));
        --k;
        btn_practice1.setText(answ1.get((k*4)+0));
        btn_practice2.setText(answ1.get((k*4)+1));
        btn_practice3.setText(answ1.get((k*4)+2));
        btn_practice4.setText(answ1.get((k*4)+3));
        }
     btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
            int selectedId = group.getCheckedRadioButtonId();
        RadioButton radioButton = (RadioButton) group.findViewById(selectedId);
                String temp = radioButton.getText().toString();
                l--;
                if (temp.equals(crrtans.get(l))){
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                txtRadio.setText("" + radioButton.getText() + " IS CORRECT");   
                txtRadio.setTextColor(Color.parseColor("#008000"));
            }else{
                //RadioButton radioButton1 = (RadioButton) findViewById(checkedId);
                   // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                    TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                    txtRadio.setText("" + radioButton.getText() + " IS INCORRECT");
                    txtRadio.setTextColor(Color.parseColor("#FF0000"));
            }
            }      
     });
     }
});
于 2013-02-05T12:42:49.247 に答える