2012-11-23 34 views
9

Tôi dường như gặp sự cố khi bắt đầu nhấp vào thanh xếp hạng của mình. Thanh xếp hạng chỉ hiển thị tốt và có giá trị mặc định. Vấn đề duy nhất là tôi không thể thay đổi bất kỳ giá trị nào hoặc nó không được kích hoạt. Tôi đã thử nhiều thứ khác nhau (ví dụ: cho phép bố cục, xây dựng nó hoàn toàn bằng java). Không ai trong số họ dường như có tác động. Đây là hóa thân mới nhất của tôi trong thanh xếp hạng. Tôi phải làm một cái gì đó stooopid để không thể nắm bắt các nhấp chuột.Capture RatingBar Nhấp vào

Java Code:

RatingBar showRatingBar = (RatingBar) findViewById(R.id.showRatingBar); 
    showRatingBar.setEnabled(true); 
    showRatingBar.setClickable(true); 
    showRatingBar.setRating(0); 
    showRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){ 
     @Override 
     public void onRatingChanged(RatingBar ratingBar, float rating, 
       boolean fromUser) { 
      System.out.println("showRating.buildRatingBar: " +rating); 
      ratingBar.setRating(rating); 

     }}); 
    showRatingBar.refreshDrawableState(); 

Layout:

  <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/showQualityLabel" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:text="@string/show_rating_label" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#E6E6E6" 
      android:textSize="12sp" /> 

     <RatingBar 
      android:id="@+id/showRatingBar" 
      style="?android:attr/ratingBarStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:max="5" 
      android:numStars="5" 
      android:rating="0" 
      android:stepSize="1"/> 
    </LinearLayout> 

Cảm ơn bạn trước.

Craig

Trả lời

26

setOnClickListener() không làm việc là RatingBar đè onTouchEvent() và không bao giờ để Xem chăm sóc nó, vì vậy Quan điểm thứ performClick() không bao giờ được gọi là (mà đã có thể gọi là OnClickListener).

xuất phát từ RatingBar và ghi đè onTouchEvent()

ratingBar.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_UP) { 
        float touchPositionX = event.getX(); 
        float width = ratingBar.getWidth(); 
        float starsf = (touchPositionX/width) * 5.0f; 
        int stars = (int)starsf + 1; 
        ratingBar.setRating(stars); 

        Toast.makeText(MainActivity.this, String.valueOf("test"), 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; 
      }}); 
+2

Đáng tiếc là không may mắn. Tôi vẫn không thể nhận được bất cứ điều gì để nhận ra các nhấp chuột của tôi hoặc bất kỳ thay đổi nào trong xếp hạng. Lạ?!?! – mornindew

+0

Tôi đã tìm thấy một cách để làm điều đó, hãy thử nó :) – Talha

+0

Nice !!! Đó là chính xác nó. Chìa khóa là chức năng onTouch. Cảm ơn nhiều. – mornindew