如何在一个片段中实现两个评级栏

时间:2016-03-06 23:06:37

标签: android

这里我试图实现两个评级栏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 个答案:

答案 0 :(得分:1)

你应该只使用一个onRatingChanged方法并使用参数中的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
}   
    }