2016-08-18 3 views
0

ラジオボタンの形で複数の選択肢のオプションを持つレビュー/クイズに再利用するためのクラスを作成しようとしています。したがって、/エルスの場合を投げ、私はfindViewById(選択)を交換する方法がわからないだと私はクラスにこれを適用すると、単一の活動クラスでfindViewByIdを使用する際の問題

public void onClickListenerButton(){ 
    radioGroupReview = (RadioGroup) findViewById(R.id.radioGroupReview); 
    btnReviewSubmit = (Button) findViewById(R.id.btnReviewSubmit); 

    btnReviewSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      int selection = radioGroupReview.getCheckedRadioButtonId(); 
      radio_button = (RadioButton) findViewById(selection); 

      if(selection==R.id.radioCorrectAnswer) { 
       textrReviewResult.setText("Correct!"); 
       btnReviewContinue.setVisibility(View.VISIBLE); 
       btnReviewSubmit.setVisibility(View.GONE); 

      } else { 
       textReviewResult.setText("Try Again."); 

      } 

     } 
    }); 

} 

上の問題はない以下の機能が困難であることが判明した使用

以下のロジック。

public class ReviewLogic { 

    public void onClickListenerButton(final Button btnSubmit, final Button btnContinue, final RadioGroup radioGroup,final RadioButton radioButton, final TextView result){ 

     btnSubmit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int selection = radioGroup.getCheckedRadioButtonId(); 
       radioButton = (RadioButton) findViewById(selection); 

       if(selection==R.id.radioCorrectAnswer) { 
        result.setText("Correct!"); 
        btnContinue.setVisibility(View.VISIBLE); 
        btnSubmit.setVisibility(View.GONE); 
       } else { 
        result.setText("Try Again."); 
       } 

      } 
     }); 

    } 
} 

ロジックを引き続き使用できますが、findViewByIdを除外するにはどうすればよいですか?

はありがとう

+0

私が理解する限りあなたがアクティビティにないので、 'findViewById'を書くことはできませんので、代わりのものを探したいでしょうか? –

+0

正しい。私はこれをアクティビティごとに編集するのではなく、複数のアクティビティで再利用可能なクラスとして使用したいと考えています。 – BR89

+0

'RadioButton'が' RadioGroup'の子であると仮定すると、 'radioGroup.findViewById(selection);'を使うことができます。あなたは 'radioButton'をどこで使っていますか?使用されていないようです... – TR4Android

答えて

1
あなたはとてもあなたのようなものがあります findViewByIdradioGroupを追加しようとすることができます

:あなたはonClickListenerButton目的球に活動オブジェクトを渡す必要が

radioButton = (RadioButton) radioGroup.findViewById(selection); 
+0

を使用する他の方法を調べたり、そのクラスにアクセスするアクティビティをコンストラクタパラメータとして追加することができます。 'ReviewLogic rl = new ReviewLogic(activity)' –

+1

@Randyka Yudhistiraアクティビティへの参照を保持することは良い考えではありません... – TR4Android

+0

どのようにしてif(selection == R.id.radioWrongAnswer)を解決しますか? – BR89

0

によるアクセスのRadioButton
radioButton = (RadioButton)activity.findViewById(selection); 
関連する問題