0

在应用程序中,我想创建多个按钮并同时为它们应用事件。?我已经使用以下代码创建了动态按钮,但是当我在两个按钮上应用触摸事件时,事件一次只应用 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;

请帮忙..?

4

1 回答 1

0

如果您触摸任何按钮,它只会将该按钮作为 View v,为了影响其他按钮,您不应该这样做,而不是单个按钮,将所有按钮放在布局中,然后将它们移动到触摸视图上,因此,最终它将移动您在布局内动态定义的所有动态按钮,并且它将移动布局而不是单个按钮,因此您可以移动多个按钮,将它们放置在布局内并移动该布局。

于 2012-04-16T10:51:38.157 回答