[英]Getting 0.0 value from ratingBar.getRating(); (Default Value) even though I change the rating bar value?
我已研究过这个 ,但似乎并没有为我工作。 我在警报对话框中显示评分栏。 在按下按钮时,我只得到0.0的值。 下面是代码:
rateme.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:id="@+id/ratingBar"
android:stepSize="1.0"/>
</LinearLayout>
Java.Code
//Typecase
final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
ratingBar=(RatingBar)view.findViewById(R.id.ratingBar);
AlertDialog.Builder alertDialog = null;
alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Rate and Review");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
alertDialog.setView(R.layout.rateme);
}else{
alertDialog.setMessage("Please Rate and Review");
}
alertDialog.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String myPackage = getPackageName();
String value=" "+ratingBar.getRating();
Log.d(TAG," "+value); **//Getting value 0.0 although I change ratingbar and submit.**
}
}).setNegativeButton("QUIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(true).setIcon(android.R.drawable.star_big_on).create();
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
//The toast below is not executed even when I chaned the ratingbar stars.
Toast.makeText(Front.this, "Blank", Toast.LENGTH_SHORT).show();
//Log.d(TAG,"Reached on Listener");
}
});
alertDialog.show();
}
似乎我已正确完成所有操作,但获得的值为0.0(恒定)。 我可能会误会什么?
问题出在这里
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
alertDialog.setView(R.layout.rateme);
} else {
alertDialog.setMessage("Please Rate and Review");
}
您不需要这个if / else,因为setView
是自Api版本1起的Android的一部分alertDialog.setView(R.layout.rateme);
再次使评级栏膨胀。 使用alertDialog.setView(view);
应该解决你的问题
一次给布局充气一次,保留对RatingBar的引用,然后忘记布局。
final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
// You never work with view again.
然后AlertDialog.Builder将实际显示的另一个布局(基于相同的资源ID)膨胀。
alertDialog.setView(R.layout.rateme);
使用您夸大的布局。
final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
alertDialog.setView(view);
使用对话框上下文(而不是活动上下文),以便在您进行暗活动并需要浅对话框时正确绘制对话框。
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final LayoutInflater layoutInflater = LayoutInflater.from(alertDialog.getContext());
final View view = layoutInflater.inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
alertDialog.setView(view);
扩大视图时,请始终使用最接近,最合逻辑的上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.