在浮动图像按钮上设置侦听器?

时间:2016-08-12 20:54:33

标签: android onclicklistener

我创建了浮动布局,其中包含上,下,左,右四个图像按钮。我可以访问(移动)布局,但是我无法在这四个图像按钮上设置onclicklistener。

这是我的代码:

XML:

  <ImageButton
    android:id="@+id/down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/up"
    android:layout_below="@+id/left"
    android:src="@drawable/ic_keyboard_arrow_down_black_24dp" />

 <ImageButton
    android:id="@+id/right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/down"
    android:layout_toRightOf="@+id/down"
    android:src="@drawable/ic_keyboard_arrow_right_black_24dp" />

  <ImageButton
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/up"
    android:layout_marginTop="20dp"
    android:layout_toLeftOf="@+id/down"
    android:src="@drawable/ic_keyboard_arrow_left_black_24dp" />

  <ImageButton
    android:id="@+id/up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="91dp"
    android:src="@drawable/ic_keyboard_arrow_up_black_24dp" />

Java代码:

public class FloatingWindow extends Service {
    WindowManager wm;
    RelativeLayout mInview;


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();


        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutInflater inflater =   LayoutInflater.from(getApplicationContext());
        mInview = (RelativeLayout)inflater.inflate(R.layout.prac, null, false);


       RelativeLayout.LayoutParams layoutParameteres = new RelativeLayout.LayoutParams(
               RelativeLayout.LayoutParams.WRAP_CONTENT, 700);


        final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
                600, 400, WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        parameters.gravity = Gravity.CENTER | Gravity.CENTER;
        parameters.x = 0;
        parameters.y = 0;



         Button     stop = new Button(this);
    stop.setText("Stop");
        ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        stop.setLayoutParams(btnParameters);

    mInview.addView(stop);

        wm.addView(mInview, parameters);




        mInview.setOnTouchListener(new View.OnTouchListener() {

       WindowManager.LayoutParams updatedParameters = parameters;
            double x;
            double y;
            double pressedX;
            double pressedY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        x = updatedParameters.x;
                        y = updatedParameters.y;



                        pressedX = event.getRawX();
                        pressedY = event.getRawY();

                        break;

                    case MotionEvent.ACTION_MOVE:
                        updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
                        updatedParameters.y = (int) (y + (event.getRawY() - pressedY));

                        wm.updateViewLayout(mInview, updatedParameters);

                    default:
                        break;
                }

                return false;
            }
        });

  stop.setOnClickListener(new View.OnClickListener() {
         @Override
            public void onClick(View v) {
                wm.removeView(mInview);
                stopSelf();
                System.exit(0);
            }
        });
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
    }

}

2 个答案:

答案 0 :(得分:0)

public class FloatingWindow extends Service {

    ...

    @Override
    public void onCreate() {

        ...

        mInview.findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });

        mInview.findViewById(R.id.down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });

        mInview.findViewById(R.id.right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });

        mInview.findViewById(R.id.up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ...
            }
        });

       ...
    }
}

答案 1 :(得分:0)

您可以在每个setOnClickListener上使用ImageButton方法(如其他解决方案中所述),也可以从onClickListener使您的类实现View并处理逻辑在任意视图的点击事件的onClick方法中,如下所示。

public class FloatingWindow extends Service implements View.OnClickListener{

    // ...... all other stuff

   // handle onClick event for any view of the layout
   @Override
   public void onClick(View v) {

       switch (v.getId()) {

           case R.id.down: {
               // do something id down click
               break;
           }
           case R.id.right: {
               // do something if right click
               break;
           }
           case R.id.left: {
               // do something if left click
               break;
           }
           case R.id.up: {
               // do something if up click
               break;
           }
           default: // do nothing
               break;

       }
   }

}