2017-06-19 152 views
-1

我的Android应用中有一个覆盖按钮。当用户点击按钮时,我想显示布局并与布局的视图进行交互。 目前,我展示了一个吐司。我怎样才能做到这一点 ?当用户点击覆盖按钮时弹出一个弹出框

这是我OverlayShowingService.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(); 
    } 

} 
+0

只需做另一个视图服务并显示它'onClick' –

+0

什么阻止你膨胀该布局,并通过'WindowManager#addView()'添加它,就像你做的那个按钮? – azizbekian

+0

像这样:wm.addView(MY_VIEW,params); ?但我想设置一个像这样的R.layout.overlay布局。我如何在View中转换? –

回答

1

当点击按钮发送广播,并从广播接收器onReceive方法开始活动。

创建一个广播接收器,然后注册一个广播接收器onCreate()。单击按钮时广播意图。最后,当点击叠加按钮时,接收器将打开一个活动(布局)。

如果你只是想打开一个对话框,这是一个体面的答案。 https://stackoverflow.com/a/31221355/4179914

原来你也可以为主题,这里提到 https://stackoverflow.com/a/7918720/4179914

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); 
    } 

注册广播接收机的onCreate()

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

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

    private void registerClickReceiver() { 
     LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing, 
       new IntentFilter("user.clicked.overlay.button")); 
    } 

注销广播接收机的onDestroy()

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


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

评论如果需要任何帮助,我已测试它,它的工作。 –

0

从我从这个问题的理解,你想获得一个视图形式的XML文件,并将其添加到一个窗口。

您可以使用View view = LayoutInflater.from(context).inflate(r.layout.overlay,null,false);并将viewWindowManager.addView(view)添加到窗口。