我似乎有一个问题,捕捉我的评级栏点击。收视率条显示得很好,并且有默认值。唯一的问题是我不能更改任何值,或者它没有启用。我已经尝试了很多不同的东西(例如,在布局中启用,完全在Java中构建)。它们似乎都没有影响。这是我最新的评分标准。我一定是在做些傻事,没法捕捉到咔嗒声。
Java代码:

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

布局:
         <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>

提前谢谢你。
克雷格

最佳答案

setOnClickListener()不起作用的原因是RatingBar重写OnTouchEvent()并且从不让视图处理它,因此视图performClick()从不被调用(这将调用onClickListener)。
从ratingbar派生并重写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;
            }});

07-24 09:49
查看更多