我正在开发用户可以查看和评价产品的应用。因此,每当我点击ratingButton
时,我会得到一个弹出窗口或AlertDialog,我可以在其中评定1-5颗星。然后用户应该保存或取消评级。但是,RatingBar似乎没有显示在AlertDialog中。下面的图像应该在对话框中显示星星。如何让RatingBar显示在AlertDialog中?
这是我到目前为止所做的。
ratingBtn的onClickListener
ratingBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.AlertDialogStyle);
builder .setTitle("Rate!");
View rootView = inflater.inflate(rate_layout, container, false);
final RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
String value = String.valueOf(ratingBar.getRating()*2);
Toast.makeText(activity, " " + value, Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "Saved", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setLayout(600, 400);
alert.show();
}
});
rate_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.Rate">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rateMessage"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:theme="@style/RatingBar"/>
</RelativeLayout>
styles.xml
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#626262</item>
<item name="android:textColorPrimary">#616161</item>
<item name="android:background">@color/white</item>
</style>
答案 0 :(得分:1)
您错过了builder.setView(rootView)
;
在此行之后添加此行
View rootView = inflater.inflate(rate_layout, container, false);
答案 1 :(得分:0)
为什么使用评级栏的警告对话框。您可以在下面使用: ratingBar =(ratingBar)findViewById(R.id.ratingBar); txtRatingValue =(TextView)findViewById(R.id.txtRatingValue);
//if rating value is changed,
//display the current rating value in the result (textview) automatically
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
txtRatingValue.setText(String.valueOf(rating));
}
});
答案 2 :(得分:0)
然后你必须使用自定义警报对话框。