RatingBar 不显示在 AlertDialog 中
RatingBar not displaying inside AlertDialog
我正在开发用户可以查看和评价产品的应用程序。因此,每当我单击 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>
为什么你使用评级栏的警告对话框。您可以简单地使用以下内容:
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));
}
});
然后您将不得不使用自定义警报对话框。
你错过了builder.setView(rootView)
;
在这一行之后添加这一行
View rootView = inflater.inflate(rate_layout, container, false);
我正在开发用户可以查看和评价产品的应用程序。因此,每当我单击 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>
为什么你使用评级栏的警告对话框。您可以简单地使用以下内容: 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));
}
});
然后您将不得不使用自定义警报对话框。
你错过了builder.setView(rootView)
;
在这一行之后添加这一行
View rootView = inflater.inflate(rate_layout, container, false);