2017-06-28 34 views
0

我想要顯示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) { 



       } 
      } 

    ); 

回答

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

我認爲你應該使用評級巴(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被調用,你也可以添加基於您的應用程序的消息

希望它有幫助!

相關問題