有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android studio分级栏setOnTouchListener工作不正常

我正在尝试设置一个scrollView,其中我有一个Grid layout,里面几乎没有rating bars。当我点击/触摸这些时,avarege值被发送到最后一个Rating Bar,它位于scrollView之外

但是,一切正常,每个ratingbar需要点击/触摸两次才能发送其值

知道为什么会这样吗?如果我将grid layoutscrollView移动到rating bars外部,则rating bars将按预期工作(单击/触摸一次)

Layout

我用于从评级栏发送值的代码是:

    Valutazione[0].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

            Valutazione[1].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

            Valutazione[2].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    onClickRate1();
                    return false;
                }
            });

....

public void onClickRate1()
    {

        RatingBar Valutazione[] =
                {(RatingBar) findViewById(R.id.ratingBar1), (RatingBar) findViewById(R.id.ratingBar2),
                        (RatingBar) findViewById(R.id.ratingBar3), (RatingBar) findViewById(R.id.ratingBar4),
                        (RatingBar) findViewById(R.id.ratingBar5), (RatingBar) findViewById(R.id.ratingBar6),
                        (RatingBar) findViewById(R.id.ratingBar7), (RatingBar) findViewById(R.id.ratingBar8),
                        (RatingBar) findViewById(R.id.ratingBar9), (RatingBar) findViewById(R.id.ratingBar10)
                };
        RatingBar rateTot =(RatingBar) findViewById(R.id.ratingBartot);

        float Valore[]={0,1,2,3,4,5,6,7,8,9, 10};
        Valore[0]=0;Valore[1]=0;
        Valore[2]=0;Valore[3]=0;
        Valore[4]=0;Valore[5]=0;
        Valore[6]=0;Valore[7]=0;
        Valore[8]=0;Valore[9]=0;
        Valore[10]=0;

        int j=0, divisore=0;

        TextView finalResult = (TextView) findViewById(R.id.textrate);


        while(j<=9) {

            Valore[j] = Valutazione[j].getRating();

            if(Valore[j] > 0) divisore++;

            j++;
        }

        Valore[10] = ( Valore[0] +  Valore[1] +  Valore[2] +  Valore[3] +  Valore[4] +  Valore[5] +  Valore[6] + Valore[7] +  Valore[8] + Valore[9]) / divisore;


        totalRating(rateTot, Valore[10], finalResult);

    }



    private void totalRating(RatingBar totalBar, float valoreTotale, TextView totalText) {

        totalBar.setRating(valoreTotale);
}

共 (1) 个答案

  1. # 1 楼答案

    使用setOnRatingBarChangeListener代替setOnTouchListener

    1)导入android。小装置。评级杆。OnRatingBarChangeListener

    然后:

    Valutazione[0].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
    
                    onClickRate1();
                }
    
            });