在我的Android应用中,我有一个片段显示一个RatingBar,它有一个onRatingChanged监听器,它没有响应评级栏的变化。
所以这是我的代码:
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()方法。
当前行为:即使我更改了小部件中的星标,也没有任何反应。
答案 0 :(得分:0)
试试这个。
@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 + "");
}
});
}