评分栏Android

时间:2015-09-16 22:14:11

标签: java android rating

您好我正忙着制作一个程序,您可以对人们进行评分 我一直在玩评级栏小部件但是我很难在选择明星数量后让它显示评级。我做错了什么。我还想知道android sdk是否有任何上下投票,并且将图像用于评级系统是明智的。

部分代码被注释掉的原因是因为我的应用程序在取消注释时不会运行,但我知道它需要做我想做的事。

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class rating_system_fragment extends Fragment implements OnRatingBarChangeListener{

RatingBar ratingBar;
TextView ratingResult;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    inflater.inflate(R.layout.rating_system_fragment,container,false);
    //setContentView(R.layout.activity_main);
    return inflater.inflate(R.layout.rating_system_fragment, container, false);
    //ratingResult = (TextView) ratingResult.findViewById(R.id.textViewRating);
    //((RatingBar) ratingBar.findViewById(R.id.ratingBar))
      //      .setOnRatingBarChangeListener(this);
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromTouch) {
    final int numStars = ratingBar.getNumStars();
    ratingResult.setText(rating + "/" + numStars);
}

}

1 个答案:

答案 0 :(得分:0)

您的评论代码中有多个语法错误。它应该是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.rating_system_fragment,container,false);
    ratingResult = (TextView) rootView.findViewById(R.id.textViewRating);
    ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
    ratingBar.setOnRatingBarChangeListener(this);

    return rootView;
}

这里的关键是ratingBartextViewRating是您的根视图的子项,并且只有在您充分夸大它们时才能访问它们。