gpt4 book ai didi

java - Android Studio-RatingBar setOnTouchListener无法正常工作

转载 作者:行者123 更新时间:2023-12-03 18:08:33 26 4
gpt4 key购买 nike

我试图设置一个scrollView,其中我里面有一个Grid layout,其中只有几个rating bars。当我单击/触摸这些变量时,求值将发送到最终的Rating Bar,该最终值在scrollView之外。

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

知道为什么会这样吗?如果我将grid layoutscrollView移到外面,则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);
}

最佳答案

代替setOnTouchListener使用setOnRatingBarChangeListener

1)导入android.widget.RatingBar.OnRatingBarChangeListener;

然后:

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

onClickRate1();
}

});

关于java - Android Studio-RatingBar setOnTouchListener无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com