Android 当用户单击覆盖按钮时显示弹出窗口

Android 当用户单击覆盖按钮时显示弹出窗口,android,Android,我的Android应用程序中有一个覆盖按钮。我想在用户单击按钮时显示布局并与布局视图交互。 现在,我举杯祝酒。我该怎么做 这是我的OverlyShowingService.class: public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener { private View topLeftView; private Button overlayed

我的Android应用程序中有一个覆盖按钮。我想在用户单击按钮时显示布局并与布局视图交互。 现在,我举杯祝酒。我该怎么做

这是我的OverlyShowingService.class:

public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {

    private View topLeftView;

    private Button overlayedButton;
    private float offsetX;
    private float offsetY;
    private int originalXPos;
    private int originalYPos;
    private boolean moving;
    private WindowManager wm;

    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }

    @Override
    public void onCreate() {
    super.onCreate();

    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    overlayedButton = new Button(this);
    overlayedButton.setText("Overlay button");
    overlayedButton.setOnTouchListener(this);
    overlayedButton.setBackgroundColor(Color.BLACK);
    overlayedButton.setOnClickListener(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;
    wm.addView(overlayedButton, params);

    topLeftView = new View(this);
    WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
    topLeftParams.x = 0;
    topLeftParams.y = 0;
    topLeftParams.width = 0;
    topLeftParams.height = 0;
    wm.addView(topLeftView, topLeftParams);

    }

    @Override
    public void onDestroy() {
    super.onDestroy();
    if (overlayedButton != null) {
        wm.removeView(overlayedButton);
        wm.removeView(topLeftView);
        overlayedButton = null;
        topLeftView = null;
    }
    }

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

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getRawX();
        float y = event.getRawY();

        moving = false;

        int[] location = new int[2];
        overlayedButton.getLocationOnScreen(location);

        originalXPos = location[0];
        originalYPos = location[1];

        offsetX = originalXPos - x;
        offsetY = originalYPos - y;

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int[] topLeftLocationOnScreen = new int[2];
        topLeftView.getLocationOnScreen(topLeftLocationOnScreen);

        System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
        System.out.println("originalY="+originalYPos);

        float x = event.getRawX();
        float y = event.getRawY();

        WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

        int newX = (int) (offsetX + x);
        int newY = (int) (offsetY + y);

        if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
        return false;
        }

        params.x = newX - (topLeftLocationOnScreen[0]);
        params.y = newY - (topLeftLocationOnScreen[1]);

        wm.updateViewLayout(overlayedButton, params);
        moving = true;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (moving) {
        return true;
        }
    }

    return false;
    }

    @Override
    public void onClick(View v) {
    Toast.makeText(this, "Here I want to show a layout with some options in a box", Toast.LENGTH_SHORT).show();
    }

}
公共类OverlyShowingService将服务实现扩展到OnTouchListener、OnClickListener{
私有视图topLeftView;
私有按钮覆盖按钮;
私人浮动抵消;
私人浮动补贴;
私人int原创XPOS;
私人内部原始版本;
私有布尔移动;
私有窗口管理器;
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
wm=(WindowManager)getSystemService(Context.WINDOW\u服务);
overlayedButton=新按钮(此按钮);
setText(“覆盖按钮”);
setOnTouchListener(这个);
重叠按钮。收进背景颜色(颜色。黑色);
setOnClickListener(这个);
WindowManager.LayoutParams params=新建WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.TYPE_系统_警报,WindowManager.LayoutParams.FLAG_不可聚焦| WindowManager.LayoutParams.FLAG_不可触摸(模式,像素格式.半透明);
params.gravity=gravity.LEFT | gravity.TOP;
参数x=0;
参数y=0;
wm.addView(覆盖按钮,参数);
topLeftView=新视图(此视图);
WindowManager.LayoutParams topLeftParams=新建WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.TYPE_系统_警报,WindowManager.LayoutParams.FLAG_不可聚焦| WindowManager.LayoutParams.FLAG_不可触摸|模式,像素格式.半透明);
topLeftParams.gravity=gravity.LEFT | gravity.TOP;
topLeftParams.x=0;
topLeftParams.y=0;
topLeftParams.width=0;
topLeftParams.height=0;
wm.addView(topLeftView,topLeftParams);
}
@凌驾
公共空间{
super.ondestory();
if(overlayedButton!=null){
wm.removeView(覆盖按钮);
wm.removeView(topLeftView);
overlayedButton=null;
topLeftView=null;
}
}
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
float x=event.getRawX();
float y=event.getRawY();
移动=假;
int[]位置=新int[2];
覆盖按钮。getLocationOnScreen(位置);
originalXPos=位置[0];
原始ypos=位置[1];
offsetX=原始xpos-x;
offsetY=原始ypos-y;
}else if(event.getAction()==MotionEvent.ACTION\u MOVE){
int[]topLeftLocationOnScreen=新建int[2];
topLeftView.GetLocationOn屏幕(TopLeftLocationOn屏幕);
System.out.println(“topLeftY=“+topLeftLocationOnScreen[1]);
System.out.println(“originalY=“+originalYPos”);
float x=event.getRawX();
float y=event.getRawY();
WindowManager.LayoutParams params=(LayoutParams)overlayedButton.getLayoutParams();
int newX=(int)(抵销x+x);
int newY=(int)(offsetY+y);
if(Math.abs(newX-originalXPos)<1&&Math.abs(newY-originalYPos)<1&&moving){
返回false;
}
参数x=newX-(屏幕上的topLeftLocationOnScreen[0]);
参数y=newY-(屏幕上的TopLeftLocation[1]);
wm.UpdateView布局(覆盖按钮,参数);
移动=真;
}else if(event.getAction()==MotionEvent.ACTION\u UP){
如果(移动){
返回true;
}
}
返回false;
}
@凌驾
公共void onClick(视图v){
Toast.makeText(这里是“我想在一个框中显示带有一些选项的布局”,Toast.LENGTH_SHORT.show();
}
}

根据我对问题的理解,您希望获得一个表单xml文件视图,并将其添加到窗口中


您可以使用
View-View=LayoutInflater.from(context).充气(r.layout.overlay,null,false)并使用
窗口管理器将
视图添加到窗口中。添加视图(视图)

单击按钮时发送广播,并从广播接收器onReceive方法启动活动。

创建广播接收器,然后在服务的创建()上注册广播接收器。单击按钮时广播意图。最后,单击覆盖按钮时,接收器将打开一个活动(布局)

如果你只想打开一个对话框,这是一个不错的答案

事实证明,你也可以像这里提到的那样,将一个活动主题化为一个对话框

单击“在覆盖上发送广播”按钮

注册广播接收器onCreate()

取消注册广播接收器onDestroy()


只需使用view创建另一个服务并在单击时显示它
onClick
是什么阻止您膨胀该布局并通过
WindowManager#addView()
添加它,就像您为该按钮所做的那样?比如:wm.addView(MY#view,params)?但我想设置一个类似于R.Layout.overlay的布局。如何将其转换为视图?@Jéwôm'请定义您将在弹出窗口中执行的操作?如果需要任何帮助,请发表评论,我已经测试了它,它正在工作。
public class OverlayClickReceiver extends BroadcastReceiver {

   @Override
    public void onReceive(Context context, Intent intent) {

        Intent toDialog = new Intent(context, Main2Activity.class);
        toDialog.setFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(toDialog);
    }
}
    @Override
    public void onClick(View v) {
        broadcastClick();
    }

   private void broadcastClick() {
        final Intent intent = new Intent("user.clicked.overlay.button");
        final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
        broadcastManager.sendBroadcast(intent);
    }
    @Override
    public void onCreate() {
        super.onCreate();

        overlayShowing = new OverlayClickReceiver();
        .......
    }

   private void registerClickReceiver() {
        LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing,
                new IntentFilter("user.clicked.overlay.button"));
    }
@Override
public void onDestroy() {
    super.onDestroy();
    ..........
    unregisterClickReceriver();
}


   private void unregisterClickReceriver() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(overlayShowing);
    }