繁体   English   中英

评级栏返回默认值

[英]Rating Bar returns the default Value

我的申请有问题。 在用户点击“完成按钮”之后,出现“AlertDialog”并且用户能够对某些内容进行评级。

我的问题是当用户从AlertDialog(positveButton)点击ok按钮时,评级栏返回默认值(0.0)。 我尝试使用OnRatingBarChangeListener并将评级保存在变量中但这也不起作用。


Java代码:

public RatingBar ratingBar;

AlertDialog

 AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this); final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null); // This a have from another Stackoverflow question build.setView(R.layout.dialog_rating); ratingBar = (RatingBar) view.findViewById(R.id.dialog_ratingBar); build.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dia, int which) { Log.i("[Rate]", String.valueOf(ratingBar.getRating())); //This returns 0.0 Log.i("[Rate]", "" + ratingBar); // Is Valid check } }); AlertDialog dialog = build.create(); dialog.setTitle(R.string.rating_dialog_title); ialog.show(); 

XML代码

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RatingBar style="@android:style/Widget.DeviceDefault.RatingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dialog_ratingBar" android:numStars="5" android:stepSize="0.5" android:layout_gravity="center_horizontal" /> </LinearLayout> 

谢谢你的帮助

好的,我修好了。 对于那些有同样问题的人:我改变了final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null); final View view = this.getLayoutInflater().inflate(R.layout.dialog_rating, null);

现在它工作正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM