浮动窗口不听点击

时间:2018-09-06 08:05:58

标签: java android android-windowmanager

代码

public class FloatingWindow extends Service{
WindowManager wm;
LinearLayout ll;
LayoutInflater li;

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

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    View myview;
    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            750, 1000, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.CENTER | Gravity.CENTER;
    myview = li.inflate(R.layout.service_floating_window, null); // your layout here

    wm.addView(myview, params);
    params.x = 0;
    params.y = 0;

    ImageButton close = (ImageButton) myview.findViewById(R.id.close);

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


//        Button stop = new Button(this);
//        stop.setText("Stop");
//        ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//        stop.setLayoutParams(btnParameters);
//
//        ll.addView(stop);
//       wm.addView(ll, params);
    ll = new LinearLayout(this);
    final WindowManager.LayoutParams finalParameters = params;

    ll.setOnTouchListener(new View.OnTouchListener() {
        WindowManager.LayoutParams updatedParameters = finalParameters;
        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(ll, updatedParameters);

                default:
                    break;
            }

            return false;
        }
    });

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

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

它不仅不听按钮的点击,而且不动...就像根据onTouchListener一样,它应该在被触摸和移动时四处移动...当我只是创建一个简单的对话框并移动它时,它就起作用了。 ..但后来我切换到一种布局,它无法正常工作...我错过了什么吗?

0 个答案:

没有答案