我目前正在开发一个应用程序,该应用程序应该能够检测到gridlayout中的哪些单独的textviews被触及。目标是将触摸视图的背景颜色设置为不同的颜色。我正在创建这样的观点:
for (int row = 1; row < 13; row++) {
for (int col=0; col<24; col++) {
TextView tv = new TextView(this);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(col));
params.width = (int)(screenWidth / 24);
params.height = (int) (tenth_heigth * 8) / 12 ;
tv.setLayoutParams(params);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.WHITE);
tv.setText("A");
tv.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(tv, params);
}
}
我尝试在活动中使用重写的onTouchEvent方法执行此操作,但到目前为止,我没有运气实现我的目标。最好的方法是什么?
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
// would like to set the background color of the touched view to blue
break;
case MotionEvent.ACTION_MOVE :
// would like to set the background color of the touched views to red
break;
case MotionEvent.ACTION_UP :
// set the background color to purple
break;
}
return super.onTouchEvent(event);
}
如果有人能指出我正确的方向,我将不胜感激。非常感谢!