Java Android studio-分级栏设置TouchListener工作不正常
我正在尝试设置一个Java Android studio-分级栏设置TouchListener工作不正常,java,android,android-studio,ontouchlistener,ratingbar,Java,Android,Android Studio,Ontouchlistener,Ratingbar,我正在尝试设置一个滚动视图,其中我有一个网格布局,其中几乎没有评级条。当我点击/触摸这些按钮时,avarege值被发送到最后的评级条,该评级条位于滚动视图的外部 但是,一切正常,每个分级栏都需要单击/触摸两次才能发送其值 知道为什么会这样吗?如果我将网格布局从滚动视图向外移动,分级栏按预期工作(单击/触摸一次) 我用于从评级栏发送值的代码是: Valutazione[0].setOnTouchListener(new View.OnTouchListener() {
滚动视图
,其中我有一个网格布局
,其中几乎没有评级条
。当我点击/触摸这些按钮时,avarege值被发送到最后的评级条
,该评级条位于滚动视图
的外部
但是,一切正常,每个分级栏都需要单击/触摸两次才能发送其值
知道为什么会这样吗?如果我将网格布局
从滚动视图
向外移动,分级栏
按预期工作(单击/触摸一次)
我用于从评级栏发送值的代码是:
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);
}
Valutazione[0]。setOnTouchListener(新视图。OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
onClickRate1();
返回false;
}
});
Valutazione[1]。setOnTouchListener(新视图。OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
onClickRate1();
返回false;
}
});
Valutazione[2]。setOnTouchListener(新视图。OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
onClickRate1();
返回false;
}
});
....
点击率1()时的公共无效
{
RatingBar Valutazione[]=
{(RatingBar)findViewById(R.id.ratingBar1),(RatingBar)findViewById(R.id.ratingBar2),
(评级条)findViewById(R.id.ratingBar3),(评级条)findViewById(R.id.ratingBar4),
(评级条)findViewById(R.id.RatingBar 5),(评级条)findViewById(R.id.RatingBar 6),
(评级条)findViewById(R.id.RatingBar 7),(评级条)findViewById(R.id.RatingBar 8),
(评级条)findViewById(R.id.ratingBar9),(评级条)findViewById(R.id.ratingBar10)
};
RatingBar rateTot=(RatingBar)findViewById(R.id.ratingBartot);
浮动价[]={0,1,2,3,4,5,6,7,8,9,10};
Valore[0]=0;Valore[1]=0;
瓦洛雷[2]=0;瓦洛雷[3]=0;
瓦洛雷[4]=0;瓦洛雷[5]=0;
瓦洛雷[6]=0;瓦洛雷[7]=0;
瓦洛雷[8]=0;瓦洛雷[9]=0;
Valore[10]=0;
int j=0,除数=0;
TextView finalResult=(TextView)findViewById(R.id.textrate);
而(j0)除数++;
j++;
}
Valore[10]=(Valore[0]+Valore[1]+Valore[2]+Valore[3]+Valore[4]+Valore[5]+Valore[6]+Valore[7]+Valore[8]+Valore[9])/divisore;
总评级(总评级、价值[10],最终结果);
}
私有无效总体评级(比率栏总体评级、浮动价总体评级、文本视图总体评级){
totalBar.设定值(从价总计);
}
使用setOnTouchListener
而不是setOnTouchListener
1) 导入android.widget.RatingBar.OnRatingBarChangeListener
然后:
Valutazione[0].setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
onClickRate1();
}
});