2016-03-06 54 views
0

這裏我試圖實現兩個評級欄ratingbar1和ratingbar2,但錯誤表示只能調用一個onRatingChanged,那麼我怎樣才能實現ratingbar2。如何在一個片段中實現兩個評級欄

雖然當我試圖只實施ratingbar1,我的代碼工作正常。

請告訴我如何實現ratingbar2的onRatingChanged方法?

public class FragmentT12 extends Fragment implements RatingBar.OnRatingBarChangeListener 
    { 
     View view; 
     TextView textbox1,textbox2; 
     RatingBar ratingbar1,ratingbar2; 

     @Nullable 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 

      View view= inflater.inflate(R.layout.fragment_t12,container,false); 
      textbox1=(TextView)view.findViewById(R.id.textbox1); 
      textbox2=(TextView)view.findViewById(R.id.textbox2); 
      ratingbar1=(RatingBar)view.findViewById(R.id.ratingbar1); 
      ratingbar2=(RatingBar)view.findViewById(R.id.ratingbar2); 
      ratingbar1.setOnRatingBarChangeListener(this); 
      ratingbar2.setOnRatingBarChangeListener(this); 
      return view; 
     } 

     @Override 
     public void onRatingChanged(RatingBar ratingbar1, float rating, boolean fromUser) 
     { 
      float i; 
      i=ratingbar1.getRating(); 

      if(i==1.0) 
      {textbox1.setText("Strongly Disagree"); 
      textbox1.setTextColor(Color.parseColor("#990000")); 
       textbox1.setTypeface(Typeface.SERIF); 
      } 

      else if(i==2.0) 
      {textbox1.setText("Disagree"); 
       textbox1.setTextColor(Color.parseColor("#996600")); 
       textbox1.setTypeface(Typeface.SERIF);} 

      else if(i==3.0) 
      {textbox1.setText("Neither Agree Nor Disagree"); 
       textbox1.setTextColor(Color.parseColor("#006666")); 
       textbox1.setTypeface(Typeface.SERIF);} 

      else if(i==4.0) 
      {textbox1.setText("Agree"); 
       textbox1.setTextColor(Color.parseColor("#0033cc")); 
       textbox1.setTypeface(Typeface.SERIF);} 

      else if(i==5.0) 
      {textbox1.setText("Strongly Agree"); 
       textbox1.setTextColor(Color.parseColor("#009933")); 
       textbox1.setTypeface(Typeface.SERIF);} 

     } 

     public void onRatingChanged(RatingBar ratingbar2, float rating, boolean fromUser)**ERROR IS HERE 
     { 
      float i; 
      i=ratingbar2.getRating(); 

      if(i==1.0) 
      {textbox2.setText("Strongly Disagree"); 
       textbox2.setTextColor(Color.parseColor("#990000")); 
       textbox2.setTypeface(Typeface.SERIF); 
      } 

      else if(i==2.0) 
      {textbox2.setText("Disagree"); 
       textbox2.setTextColor(Color.parseColor("#996600")); 
       textbox2.setTypeface(Typeface.SERIF);} 

      else if(i==3.0) 
      {textbox2.setText("Neither Agree Nor Disagree"); 
       textbox2.setTextColor(Color.parseColor("#006666")); 
       textbox2.setTypeface(Typeface.SERIF);} 

      else if(i==4.0) 
      {textbox2.setText("Agree"); 
       textbox2.setTextColor(Color.parseColor("#0033cc")); 
       textbox2.setTypeface(Typeface.SERIF);} 

      else if(i==5.0) 
      {textbox2.setText("Strongly Agree"); 
       textbox2.setTextColor(Color.parseColor("#009933")); 
       textbox2.setTypeface(Typeface.SERIF);} 

     } 
    } 

回答

1

你應該只使用一個onRatingChanged方法,並檢查其中的RatingBar呼喚它,在參數使用的RatingBar:

@Override 
public void onRatingChanged(RatingBar ratingbar, float rating, boolean fromUser){ 
if(ratingbar== ratingbar1){ 
//code for ratingbar 1 
} 
else if (ratingbar == ratingbar2){ 
//code for ratingbar 2 
} 
    } 
+0

Thanks.It工作。 –

相關問題