如何获得在Android上的指针/光标位置 [英] how to get the pointer/cursor location in android

查看:2264
本文介绍了如何获得在Android上的指针/光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的,我想获取/返回光标位置(lattitude和langitude)当我点击屏幕上的任何地方。任何一个可以帮助我..?

in android I would like to get/return the cursor location (lattitude and langitude) when i click anywhere on the screen . Can any one help me..?

推荐答案

我不知道你想获得lattitude和langitude什么,但我相信你可以得到cordinates的位置 X和Y 当用户触摸屏幕。

I don't know for what you want to get lattitude and langitude but I am sure you can get the position of the cordinates X and Y when a user touch the screen.

实施 OnTouchListener 活动,并设置监听器你的浏览使用 setOnTouchListener()方法。

Implement OnTouchListener in your Activity and set the Listener for your View using the setOnTouchListener() method.

现在我们可以覆盖在你的活动的 onTouch()方法

Now you can override the onTouch() method in your Activity.

@Override
public boolean onTouch(View view, MotionEvent event) {
    int x = (int) event.getX();       
    int y = (int) event.getY();
    return false;
}

请参阅我写了一个示例代码段为你:

See I have written a sample snippet for you:

    public class OnTouchDemo extends Activity implements OnTouchListener {

    private TextView tView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tView = (TextView) findViewById(R.id.txtView1);
        tView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        Log.v("OnTouchDemo", "X==" + String.valueOf(x) + " Y==" + String.valueOf(y));

        return false;
    }
}

这篇关于如何获得在Android上的指针/光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆