我想在不同评级中显示2个吐司。当用户点击提交按钮时,它会显示toast msg。如果是5星级显示Toast“msg1”。 如果少于5颗星,则更改Toast“msg2”。
这是我的代码。
dialogView.findViewById(R.id.review_rating).setOnTouchListener
(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float touchPositionX = event.getX();
float width = review_rating.getWidth();
float starsf = (touchPositionX / width) * 5.0f;
int stars = (int) starsf + 1;
if (stars > 5) {
stars = 5;
}
rating_stars = stars + "";
review_rating.setRating(stars);
Toast.makeText(DetailActivity.this, stars + " rate", Toast.LENGTH_SHORT).show();
v.setPressed(false);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
v.setPressed(false);
}
return true;
}
});
dialogBuilder.setNegativeButton("Cancel", null);
dialogBuilder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}
);
答案 0 :(得分:2)
我认为你应该使用评级栏(https://developer.android.com/reference/android/widget/RatingBar.html)代码。它会根据用户的评分动态向用户显示消息。
在您的activity.xml中添加:
<RatingBar
android:id="@+id/rating2"
android:layout_marginLeft="30dp"
android:numStars="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在您的activity.java中,onCreate方法添加:
RatingBar r2 = (RatingBar) findViewById(R.id.rating2);
r2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar r2, float rating, boolean fromUser) {
if(rating<=2)
Toast.makeText(getApplicationContext(), "Bad!", Toast.LENGTH_SHORT).show();
else if(rating<=3.5)
Toast.makeText(getApplicationContext(), "Unsatisfactory", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Great", Toast.LENGTH_SHORT).show();
}
});
评级一旦更改,就会调用RatingBarChangeListener,您也可以根据应用程序添加消息
希望它有所帮助!!
答案 1 :(得分:1)
使用像这样的东西..
dialogView.findViewById(R.id.review_rating).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float touchPositionX = event.getX();
float width = review_rating.getWidth();
float starsf = (touchPositionX / width) * 5.0f;
int stars = (int) starsf + 1;
if (stars > 5) {
stars = 5;
}
rating_stars = stars + "";
review_rating.setRating(stars);
if(stars>=5){
Toast.makeText(DetailActivity.this, "MESSAGE 1", Toast.LENGTH_SHORT).show();
}
else if(stars<5){
Toast.makeText(DetailActivity.this, "MESSAGE 2", Toast.LENGTH_SHORT).show();
}
v.setPressed(false);
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
v.setPressed(false);
}
return true;
}
});