本文介绍了Android的定位元素在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图中,当用户presses一个按钮,我想收集按钮的坐标,并把我夸大了右它上面的屏幕上一个EditText。当用户点击任何其他地方在屏幕上的EditText会消失,这将会触发使用该用户输入到框中的数据的方法。我怎么会去这样做这样的事情?我想类似QuickActions的东西,但不是很侵扰。有人能指出我至少如何去获取按钮坐标方向?

I have a listview and when the user presses a button I want to gather the coordinates of the button and place an edittext that I inflate right over top of it on the screen. When the user clicks anywhere else on the screen the edittext will disappear and it will fire a method that uses the data the user entered into the box. How would I go about doing something like this? I would like something similar to QuickActions, but not quite as intrusive. Could someone point me in the direction of at least how to go about getting the button coordinates?

推荐答案

好了,所以这里是如何我已经能够做到什么,我希望做。是否可以动态地放置PopupWindow而不必惹调节利润等。

Ok, so here is how i've been able to achieve what i'm looking to do. Is it possible to dynamically place a PopupWindow without having to mess with adjusting margins etc.

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是我选择了PopupWindow的大小和vHeight是我从上下文的onCreate收集主父视图的高度。请记住,这是不是打磨code。我还需要补充一些像动画和退出,以及一个可爱的小箭头,或什么显示什么窗口被关联。该setBackgroundDrawable是非常重要的,如果你不使用它,你将不能够条条框框点击将其关闭。

The pWidth and pHeight are the size of the PopupWindow I chose and the vHeight is the height of the main parent view that I collected from the onCreate context. Keep in mind this is not polished code. I still need to add a few things like animate in and out as well as a nice little arrow or something to show what the window is being associated with. The setBackgroundDrawable is very important and if you don't use it you won't be able to click outside the box to close it.

目前,它的怪异。我有两次单击框外关闭窗口。第一次单击似乎只是强调我的文本框和第二次点击实际关闭它。任何人有任何想法,为什么这可能发生?

Right now, its weird. I have to click twice outside the box to close the window. The first click just seems to highlight my textbox and the second click actually closes it out. Anyone have any idea why that might be happening?

这篇关于Android的定位元素在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:39
查看更多