2016-03-06 25 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工作。 –

相关问题