屏幕上的Android位置元素

屏幕上的Android位置元素,android,window,Android,Window,我有一个listview,当用户按下一个按钮时,我想收集按钮的坐标,并在屏幕上放置一个edittext,我将它膨胀到顶部。当用户单击屏幕上的任何其他位置时,edittext将消失,并触发一个使用用户输入框中的数据的方法。我该怎么做这样的事情呢?我想要一些类似于QuickActions的东西,但不太具有侵入性。有人能告诉我至少该如何获取按钮坐标吗?混乱,取决于您的视图层次结构。该方法(以及getRight、getTop和getBottom)都与控件的视图父级相关。看一看,看看它是否能满足你的要求。

我有一个listview,当用户按下一个按钮时,我想收集按钮的坐标,并在屏幕上放置一个edittext,我将它膨胀到顶部。当用户单击屏幕上的任何其他位置时,edittext将消失,并触发一个使用用户输入框中的数据的方法。我该怎么做这样的事情呢?我想要一些类似于QuickActions的东西,但不太具有侵入性。有人能告诉我至少该如何获取按钮坐标吗?

混乱,取决于您的视图层次结构。该方法(以及getRight、getTop和getBottom)都与控件的视图父级相关。看一看,看看它是否能满足你的要求。

好的,下面是我如何实现我所期望的目标的。是否可以动态放置弹出窗口而不必调整边距等

public void showPopup(View view, View parentView, final int getId, String getLbs){
    int pWidth = 100;
    int pHeight = 80;
    int vHeight = parentView.getHeight(); //The listview rows height.
    int[] location = new int[2];

    view.getLocationOnScreen(location);
    final View pView = inflater.inflate(R.layout.list_popup, null, false);
    final PopupWindow pw = new PopupWindow(pView, pWidth, pHeight, false);
    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setOutsideTouchable(true);
    pw.setBackgroundDrawable(new BitmapDrawable());
    pw.showAtLocation(view, Gravity.NO_GRAVITY, location[0]-(pWidth/4), location[1]+vHeight);

    final EditText input = (EditText)pView.findViewById(R.id.Input);
    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Log.i("Focus", "Focus Changed");
            if (hasFocus) {
                //Shows the keyboard when the EditText is focused.
                InputMethodManager inputMgr = (InputMethodManager)RecipeGrainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                inputMgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
            }

        }
    });
    input.setText("");
    input.requestFocus();
    Log.i("Input Has Focus", "" + input.hasFocus());
    pw.setOnDismissListener(new OnDismissListener(){

        @Override
        public void onDismiss() {
            changeWeight(getId, Double.parseDouble(input.getText().toString()));
            Log.i("View Dismiss", "View Dismissed");
        }

    });

    pw.setTouchInterceptor(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                Log.i("Background", "Back Touched");
                pw.dismiss();
                return true;
            }
            return false;
        }
    });
}
pWidth和pHeight是我选择的弹出窗口的大小,vHight是我从onCreate上下文收集的主父视图的高度。请记住,这不是经过修饰的代码。我仍然需要添加一些东西,比如动画输入和输出,以及一个漂亮的小箭头或其他东西来显示窗口与之相关的内容。setBackgroundDrawable非常重要,如果您不使用它,您将无法单击框外以关闭它


现在,很奇怪。我必须在框外单击两次才能关闭窗口。第一次点击似乎只是突出显示我的文本框,第二次点击实际上关闭了它。有人知道为什么会发生这种情况吗?

getLocationOnScreen似乎为我提供了x和y坐标。实际上,我一直在研究如何将视图充气并将其放置在屏幕上。有什么想法吗?这取决于基本的视图组:如果您使用的是LinearLayout或RelativeLayout之类的工具,那么绝对定位就没有好方法了。您可以尝试在左上角位置进行充气,将大小设置为所需值,然后根据x和y坐标设置边距以将其移动到位。非常混乱,但是…所以没有SetAthisLocation(x,y)方法可以在视图对象上使用?我会仔细考虑你的想法,然后公布我的结果。谢谢