[英]Rating Bar Android
Hi I am busy making a program where you get to rate people I've been playing around with the Rating Bar widget however I am struggling to make it show a rating after selecting the amount of stars. 嗨,我正在忙着制作一个程序,您可以在此使用评分栏小部件对我一直在玩的人进行评分,但是在选择星级后,我一直在努力使其显示评分。 What I'm I doing wrong. 我做错了。 I would also like to to know if the android sdk have anything on up and down votes and would it be wise to use images for a rating system. 我还想知道android sdk是否在上下表决中有任何东西,将图像用于评级系统是否明智?
The reason why some of the code is commented out is because my app won't run when its uncommented but I know its needed to do what I want to do. 之所以将部分代码注释掉,是因为我的应用程序在未注释时不会运行,但是我知道它需要执行我想做的事情。
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);
}
} }
You have several syntax erros in your commented code. 您的注释代码中有几个语法错误。 It should look like this: 它看起来应该像这样:
@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;
}
The key here is that ratingBar
and textViewRating
are childs of your root view, and should only be accessed when you have aready inflated them. 这里的关键是, ratingBar
和textViewRating
是你的根视图的孩子的,而当你已经AREADY夸大他们只能进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.