如何添加不同评级的 2 个吐司?

How to add 2 toast in different ratings?

我想在不同的 rating.When 中显示 2 个吐司,用户点击提交按钮,它显示吐司消息。如果是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) {



                }
            }

    );

使用类似这样的东西..

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;
    }
    });

我认为你应该为你的代码使用 ratings bar(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 就会被调用,您还可以根据您的应用程序添加消息

希望对您有所帮助!!