由于显示超过 5 颗星,我正在RatingBar
动态创建一个并将其分配给警报对话框。这是代码:
rater
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//How to get the rating value here
});
rater.setNegativeButton("Abbrechen", null);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View final layout = inflater.inflate(R.layout.ratinglayout,null);
rater.setView(layout);
//rater.setView(getLayoutInflater().inflate(R.layout.ratinglayout, null));
rater.show();
XML 布局为RatingBar
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/ratingStars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:layout_gravity = "center_horizontal"
/>
</LinearLayout>
如何在警报对话框的 OK 处理程序中获取评级值?我尝试了以下方法:
RatingBar rating = (RatingBar) findViewById(R.id.ratingStars);
但是当我这样做时,我会收到一个空指针异常警告。因此,问题是如何获得评分值?