本文介绍了Android RatingBar onRatingChanged没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Android应用中,我有一个片段,显示了RatingBar,其中有一个onRatingChanged侦听器,该侦听器未响应Ratingbar的更改.
In my Android app I've got a Fragment showing a RatingBar, which has a onRatingChanged listener that is not responding to ratingbar changes.
这是我的代码:
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);
}
}
预期的行为: onRatingChanged()方法被调用.
EXPECTED BEHAVIOR: The onRatingChanged() method gets invoked.
当前行为::即使我更改了小部件中的星星,也没有任何反应.
CURRENT BEHAVIOR: Even if I change the stars in the widget, nothing happens.
推荐答案
尝试一下.
@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 + "");
}
});
}
这篇关于Android RatingBar onRatingChanged没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!