[英]rating bar is not showing in alert dialog
I'm creating an app in that I'm allow client to rate the app.我正在创建一个应用程序,我允许客户对应用程序进行评分。
I have two issues我有两个问题
FIRST .首先。
I have added Rating bar in Alert dialog but dont know why its getting overwrite on button which i have created for submit the ratings.我在警报对话框中添加了评级栏,但不知道为什么它会覆盖我为提交评级而创建的按钮。 I have tried with margin-top for button still its not working
我已经尝试使用 margin-top for button 仍然无法正常工作
Second.第二。
On submit it also not showing toast message.提交时也没有显示吐司消息。
Code for Alert Dialog警报对话框代码
if (id == R.id.nav_rate) {
AlertDialog.Builder mBuild = new AlertDialog.Builder(homeActivity.this);
View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);
final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(homeActivity.this, ((int) rating),Toast.LENGTH_LONG).show();
}
});
}
});
mBuild.setView(mView);
AlertDialog dialog=mBuild.create();
dialog.show();
}
layout file布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:numStars="5"
android:rating="1"
android:stepSize="0.5"
android:layout_above="@+id/btnSubRating"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/btnSubRating"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
public class homeActivity extends AppCompatActivity {
private float rateValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder mBuild = new AlertDialog.Builder(homeActivity.this);
View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);
final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
rateValue = rating;
Toast.makeText(homeActivity.this, ""+rating, Toast.LENGTH_SHORT).show();
}
});
Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(homeActivity.this, ""+rateValue, Toast.LENGTH_SHORT).show();
}
});
mBuild.setView(mView);
AlertDialog dialog=mBuild.create();
dialog.show();
}
}
XML XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rate_parent"
android:weightSum="10">
<RatingBar
android:layout_marginBottom="30dp"
android:layout_weight="5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="@+id/ratingBar"
android:numStars="5"
android:rating="1"
android:stepSize="0.5"
android:layout_above="@+id/btnSubRating"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<Button
android:layout_weight="5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Submit"
android:id="@+id/btnSubRating"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
</LinearLayout>
you have fixed the size of dialog which is not allowing rating bar to fit into it.您已经修复了对话框的大小,该大小不允许评分栏放入其中。
replace dialog.getWindow().setLayout(600, 400);
替换
dialog.getWindow().setLayout(600, 400);
with dialog.getWindow().setLayout(800, 600);
与
dialog.getWindow().setLayout(800, 600);
In your Activity在您的活动中
create class variable :-创建类变量:-
float rating=0;
and use it to store rating on click of rating并使用它来存储评级点击评级
AlertDialog.Builder mBuild = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);
final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
rating=v;
}
});
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(homeActivity.this, String.valueOf(ratebar.getRating()),Toast.LENGTH_LONG).show();
}
});
mBuild.setView(mView);
AlertDialog dialog=mBuild.create();
dialog.getWindow().setLayout(600, 400);
dialog.show();
}
Try just:试试吧:
AlertDialog alertDialog = mBuild.show();
instead:反而:
AlertDialog dialog=mBuild.create();
dialog.getWindow().setLayout(600, 400);
dialog.show();
Moreover: android:layout_above are only allowed on RelativeLayout (so delete it)此外: android:layout_above 只允许在 RelativeLayout 上(所以删除它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.