如何添加2个烤面包在不同的评分?

问题描述:

我想要显示2个不同等级的烤面包。当用户点击提交按钮时,它会显示烤面包味精。如果是5星显示Toast“msg1”。 如果少于5星,请更改Toast“msg2”。如何添加2个烤面包在不同的评分?

这是我的代码。

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

我认为你应该使用评级巴(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被调用,你也可以添加基于您的应用程序的消息

希望它有帮助!