简体   繁体   English

getX()对按钮不起作用?

[英]getX() not working for button?

I used the following method for getting x and y values of my button.It returns 0 always.What might be the problem ? 我使用以下方法获取按钮的x和y值。它始终返回0。这可能是什么问题?

btn_show.setOnClickListener(new OnClickListener()
             {
                  @Override
                 public void onClick(View arg0) 
                 {
                       int x = (int)btn_show.getX();
                       int y=  (int)btn_show.getY();

                    Toast.makeText(getApplicationContext(), "button x is......"+x,Toast.LENGTH_SHORT ).show();
                    Toast.makeText(getApplicationContext(), "button y is......"+y,Toast.LENGTH_SHORT ).show();
     }
               });

I used this method to pass the value to another activity 我使用这种方法将值传递给另一个活动

Intent i = new Intent(getApplicationContext(), CustomDialogExample.class);
                    i.putExtra("value",popup_height);
                    startActivity(i);

And this method to retrieve the value 而这种方法来获取值

Intent i = getIntent();
         int intValue = i.getIntExtra("value", 0);

Finally this to set my popup position 最后这设置我的弹出位置

Window window = customizeDialog.getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.y =intValue;
            window.setAttributes(wlp);

The variable intValue returns 0 always 变量intValue始终返回0

You put the 'measuring' in the onWindowFocusChanged()-method. 您将“测量”放在onWindowFocusChanged()方法中。 As the documentation states: 如文档所述:

This is the best indicator of whether this activity is visible to the user. 这是该活动是否对用户可见的最佳指示。

You could also put it in the onResume() which is the last step before the application is completely on screen and active, however: 您也可以将其放在onResume()中,这是在应用程序完全显示在屏幕上并处于活动状态之前的最后一步,但是:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; 请记住,onResume并不是最好的指示,表明用户可以看到您的活动; a system window such as the keyguard may be in front. 系统窗口(例如键盘锁)可能位于前面。 Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game). 使用onWindowFocusChanged(boolean)可以确定您的活动对用户可见(例如,恢复游戏)。

If the window/view has not yet been displayed there is no guarantee that it has its measurements, thus the previous method would be better. 如果尚未显示窗口/视图,则不能保证它具有其测量值,因此前一种方法会更好。

Use this. 用这个。

btn_show.setOnTouchListener(new OnTouchListener() {

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();

   Toast.makeText(getApplicationContext(), "button x is......"+x,Toast.LENGTH_SHORT ).show();
   Toast.makeText(getApplicationContext(), "button y is......"+y,Toast.LENGTH_SHORT ).show();

  return false;
  }
  });

你能先尝试一下吗

Log.d("Button","Button description :"+btn_show.toString());

Try printing float value as it returned by getX() and getY(). 尝试打印由getX()和getY()返回的浮点值。 the problem might be with integer conversion. 问题可能出在整数转换上。 I tried your code and its working fine with me. 我尝试了您的代码,并与我一起正常工作。 check with your layout. 检查您的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM