在应用程序中,我想创建多个按钮并同时为它们应用事件。?我已经使用以下代码创建了动态按钮,但是当我在两个按钮上应用触摸事件时,事件一次只应用 1 个按钮
我在下面的代码中创建了多个按钮..
for(int ii= 0;ii <6;ii++)
{
params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
switch(ii)
{
case 0:
params.setMargins(100,200, 0, 0);
break;
case 1:
params.setMargins(350,200, 0, 0);
break;
case 2:
params.setMargins(600,200, 0, 0);
break;
case 3:
params.setMargins(300,400, 0, 0);
break;
case 4:
params.setMargins(100,500, 0, 0);
break;
case 5:
params.setMargins(350,500, 0, 0);
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
FrameLayout.LayoutParams params = null;
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
System.out.println("************event.getAction()******"+event.getRawX()+"*********"+event.getRawY());
params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins((int)(event.getRawX() - (v.getWidth() / 2))-25, (int)(event.getRawY() - (v.getHeight()/2))-25,0,0);
params.width =100;
params.height=100;
v.setLayoutParams(params);
break;
case MotionEvent.ACTION_MOVE:
Staticmethodclass.changecurrentplay = "Yes";
if(event.getRawX() < 780 && event.getRawY() < 1200 && event.getRawX()>=0 && event.getRawY()>=0)
{
params =new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins((int)(event.getRawX() - (v.getWidth() / 2)), (int)(event.getRawY() - (v.getHeight()/2)),0,0);
params.width =100;
params.height=100;
v.setLayoutParams(params);
}
break;
case MotionEvent.ACTION_UP:
System.out.println("*****actionup****event.getAction()******"+event.getRawX()+"*********"+event.getRawY());
if(event.getRawX() < 780 && event.getRawY() < 1200 && event.getRawX()>=0 && event.getRawY()>=0)
{
params =new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// params=(android.widget.FrameLayout.LayoutParams) v.getLayoutParams();
params.setMargins((int)(event.getRawX() - (v.getWidth() / 2))+25, (int)(event.getRawY() - (v.getHeight()/2))+25,0,0);
params.width = 50;
params.height= 50;
v.setLayoutParams(params);
}
else
{
System.out.println("***else***event.getAction()******"+event.getRawX()+"*********"+event.getRawY());
android.widget.FrameLayout.LayoutParams params1 =(android.widget.FrameLayout.LayoutParams) v.getLayoutParams();
params1.setMargins((int)params1.leftMargin+25, (int)(params1.topMargin+25),0,0);
params1.width = 50;
params1.height= 50;
v.setLayoutParams(params1);
}
return false;
请帮忙..?