2017-01-08 12 views
0

私はユーザーが製品を見たり評価したりできるアプリを開発中です。だから、私がratingButtonをクリックするたびに、私はポップアップかAlertDialogを得て、私は1から5の星を評価することができます。ユーザーは評価を保存またはキャンセルする必要があります。しかし、RatingBarはAlertDialogの中に表示されていないようです。下の画像は、ダイアログに星印を表示する必要があります。 RatingBarをAlertDialogに表示させるにはどうしたらいいですか?AlertDialogの中にRatingBarが表示されない

RatingBar not displayed

これは私がこれまで持っているものです。 ratingBtn

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> 

答えて

1

builder.setView(rootView)が見つかりませんでした。

私はalertdialog内部ratingBarをしたい、この行の後

View rootView = inflater.inflate(rate_layout, container, false); 
0

ため

onClickListenerあなたは評価バーの警告ダイアログを使用して、なぜ。あなたは単に以下を使用することができます: 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)); 

    } 
}); 
+0

をこの行を追加します。 –

0

カスタムアラートダイアログを使用する必要があります。

関連する問題