问题
In my Android app I've got a Fragment showing a RatingBar, which has a onRatingChanged listener that is not responding to ratingbar changes.
So here's my code:
rating_fragment.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rating_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RatingBar
android:id="@+id/rating_fragment_ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:stepSize="1"
/>
<EditText
android:layout_marginTop="16dp"
android:id="@+id/rating_fragment_whatswrong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/rating_fragment_whatswrong"
android:layout_gravity="center_horizontal"
android:visibility="gone"
tools:visibility="visible"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
RatingFragment.java
public class RatingFragment extends DialogFragment implements RatingBar.OnRatingBarChangeListener{
private static final String TAG = RatingFragment.class.getSimpleName();
private View rootView;
private EditText mWhatsWrong;
@NonNull
@Override
public Dialog onCreateDialog (Bundle savedInstanceState) {
// Create a new AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
// Get the layout inflater
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
rootView = layoutInflater.inflate(R.layout.rating_fragment,null);
// Inflate the layout
builder.setView(R.layout.rating_fragment);
builder.setTitle(getString(R.string.rating_fragment_title));
initialize();
return builder.create();
}
@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.rating_fragment,container,false);
return rootView;
}
private void initialize(){
RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating_fragment_ratingbar);
mWhatsWrong = (EditText) rootView.findViewById(R.id.rating_fragment_whatswrong);
ratingBar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged (RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(getContext(), String.valueOf(rating), Toast.LENGTH_SHORT).show();
if(rating < 4.0f){
mWhatsWrong.setVisibility(View.VISIBLE);
} else {
mWhatsWrong.setVisibility(View.GONE);
}
}
EXPECTED BEHAVIOR: The onRatingChanged() method gets invoked.
CURRENT BEHAVIOR: Even if I change the stars in the widget, nothing happens.
回答1:
Try this.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Create a new AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
// Get the layout inflater
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
rootView = layoutInflater.inflate(R.layout.rating_fragment, null);
// Inflate the layout
builder.setView(R.layout.rating_fragment);
builder.setTitle(getString(R.string.rating_fragment_title));
// edited here
// initialize();
return builder.create();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.rating_fragment, container, false);
// edited here,add initialize() method
initialize();
return rootView;
}
private void initialize() {
// edited here,add log info
Log.e("RatingBar", "initialize start");
RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating_fragment_ratingbar);
mWhatsWrong = (EditText) rootView.findViewById(R.id.rating_fragment_whatswrong);
// edited here
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) {
if (rating < 4.0f) {
mWhatsWrong.setVisibility(View.VISIBLE);
} else {
mWhatsWrong.setVisibility(View.GONE);
}
Log.e("RatingBar", rating + "");
}
});
}
来源:https://stackoverflow.com/questions/45690580/android-ratingbar-onratingchanged-doesnt-get-called