Android 从ratingBar.getRating()获取0.0值;(默认值)即使我更改了评级栏的值?

Android 从ratingBar.getRating()获取0.0值;(默认值)即使我更改了评级栏的值?,android,ratingbar,Android,Ratingbar,我已经调查过了,但似乎不适合我。我正在警报对话框中显示分级栏。按下按钮时,我仅获得值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

我已经调查过了,但似乎不适合我。我正在警报对话框中显示分级栏。按下按钮时,我仅获得值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>
似乎我做的每件事都是正确的,但我得到的值是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,因为自从Api版本1以来,
setView
就是Android的一部分。另一方面,由于
alertDialog.setView(R.layout.rateme),您的评级与您在对话框中看到的不同再次为分级杆充气。使用
alertDialog.setView(视图)应该解决您的问题

出了什么问题 将布局充气一次,保留对比率栏的引用,然后忘记布局

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);

膨胀视图时,始终使用最接近、最符合逻辑的上下文。

您在哪里更改值?@Blackbelt当我们在星星上滑动时,将弹出一个评级栏并更改值。对不对?这是一个例子,你确定吗?根据文件规定,API 21中添加了setView。但我想这没有任何意义,因为我在吃棉花糖。RatingBar视图很好。问题是,在我更改评级栏后,我得到的只是0.0浮点值事件。非常确定。读一下
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);