Android 评分栏点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3443939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:13:46  来源:igfitidea点击:

RatingBar onClick

androidonclickrating

提问by Andrew

I have a ListView that uses different XML files to create Views and make items out of. One of these XML files contains a RatingBar. Everything displays and looks excellent.

我有一个 ListView,它使用不同的 XML 文件来创建视图和制作项目。这些 XML 文件之一包含一个 RatingBar。一切都显示和看起来都很棒。

I'm trying to attach an onClick handler to the RatingBar to launch a new Activity. My RatingBar is of style ?android:attr/ratingBarStyleSmall; so it's just an indicator (I want the small RatingBar click to take the user to an Activity where they can do various ratings).

我正在尝试将 onClick 处理程序附加到 RatingBar 以启动新活动。我的 RatingBar 是风格 ?android:attr/ratingBarStyleSmall; 所以它只是一个指标(我希望小的 RatingBar 单击将用户带到他们可以进行各种评级的活动)。

My problem is the onClick handler for the RatingBar never gets executed. What makes it more interesting is that I've used the same code to make a LinearLayout clickable and it works fine. Could anyone tell me why?

我的问题是 RatingBar 的 onClick 处理程序永远不会被执行。更有趣的是,我使用相同的代码使 LinearLayout 可点击,并且工作正常。谁能告诉我为什么?

My Adapter's getView looks as such:

我的适配器的 getView 看起来像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    // get the View for this list item
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            // ...
            case TYPE_LOOKUP:
                v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);
                LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);
                if (vStore != null) {
                    vStore.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER WORKS FINE
                                Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);
                                startActivity(intentLaunchLookup);
                            }
                        });
                }
                break;
            case TYPE_SEPARATOR:
                v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);
                RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);
                if (r != null) {
                    r.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER DOES NOT GET EXECUTED (r IS NOT NULL; SO THIS SHOULD HAVE BEEN CREATED)
                                Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);
                                startActivity(intentLaunchRating);
                            }
                        });
                }
                break;
            // ...
        }
    }
    // …

  // return the created view
    return v;
}

回答by jonasb

The reason for setOnClickListener()not working is that RatingBaroverrides onTouchEvent()(actually its super class, AbsSeekBar, does) and never let Viewtake care of it, so View#performClick()is never called (which would have called the OnClickListener).

setOnClickListener()不工作的原因是RatingBar覆盖onTouchEvent()(实际上是它的超类,,AbsSeekBar确实)并且永远不会View照顾它,所以View#performClick()永远不会被调用(它会调用OnClickListener)。

Two possible workarounds:

两种可能的解决方法:

    derive from RatingBarand override onTouchEvent()
    派生自RatingBar和覆盖onTouchEvent()
    use OnTouchListenerinstead, like so:
    ratingBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // TODO perform your action here
            }
            return true;
        }
    
    使用OnTouchListener代替,就像这样:
    ratingBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // TODO perform your action here
            }
            return true;
        }
    

HTH, Jonas

HTH,乔纳斯

回答by Tano

Andrew, you can just try to use another event available for RatingBar, OnRatingBarChangeListener. See this example, it works for me!

安德鲁,你可以尝试使用可用于其他事件的RatingBarOnRatingBarChangeListener。看这个例子,它对我有用!

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            viewMarketDetails(mContext);
            dialog.dismiss();
        }
    });

So you don't need to use the onClickEvent but you have the same effect, but only work, off course, if you change actually the value. (If you click on the actual value, if has no effect)

所以你不需要使用 onClickEvent 但你有相同的效果,但只有在你实际更改值时才有效。(如果点击实际值,如果没有效果)

回答by Rishi Paul

ratingBar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            String rateValue = String.valueOf(ratingbar1.getRating());
            System.out.println("Rate for Module is"+rateValue);
        }
    });             

回答by Andrew

I've hacked around this problem by wrapping the RatingBar in a LinearLayout and attaching an onClick listener to the LinearLayout. I don't like doing this, but it works. For some mysterious reason onClick will not execute for a RatingBar.

我通过将 RatingBar 包装在 LinearLayout 中并将 onClick 侦听器附加到 LinearLayout 来解决这个问题。我不喜欢这样做,但它有效。由于一些神秘的原因 onClick 不会为 RatingBar 执行。

回答by Cheryl Simon

In a ListView, you generally have to decide if you want the list view items to be clickable, or an item contained within the listview. Does r.isClickable() return true?

在 ListView 中,您通常必须决定是希望列表视图项目可点击,还是包含在列表视图中的项目。r.isClickable() 是否返回 true?

You might have better luck setting clickable to false on the RatingBar, and instead using onClickListener of the listView to deal with the click events on the listView row.

您可能有更好的运气在 RatingBar 上将 clickable 设置为 false,而是使用 listView 的 onClickListener 来处理 listView 行上的点击事件。