首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取ActionEvent的坐标

获取ActionEvent的坐标
EN

Stack Overflow用户
提问于 2017-04-04 05:16:28
回答 1查看 245关注 0票数 0

我有个小麻烦。在下面的示例中,第一个TextView必须显示MotionEvent的类型-它工作得很好。第二个TextView必须显示MotionEvent的坐标,但是它不能工作。我不知道为什么,也许这只是个小错误?有人有主意吗?谢谢你的帮忙!以下是守则:

代码语言:javascript
复制
package de.androidnewcomer.motionevent;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import static android.R.attr.x;
import static android.R.attr.y;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FrameLayout Spielbereich=(FrameLayout)findViewById(R.id.Spielbereich);
    Spielbereich.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    TextView textView1=(TextView)findViewById(R.id.textView1);
    TextView textView2=(TextView)findViewById(R.id.textView2);
    TextView textView3=(TextView)findViewById(R.id.textView3);
    TextView textView4=(TextView)findViewById(R.id.textView4);
    int x1,x2,y1,y2;
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            x1 = (int)event.getX();
            y1 = (int)event.getY();
            textView1.setText("Action Down");
            textView2.setText(x1,y1);
            return true;
        }
        case MotionEvent.ACTION_UP: {
            x2 = (int)event.getX();
            y2 = (int)event.getY();
            textView3.setText("Action Up");
            textView4.setText(x2,y2);
            return true;
        }
    } return false;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 05:26:55

我认为你使用setText(...)是错误的。在文档中,您可以看到TextView有以下setText方法:

  • final void setText(int resid)使用字符串资源标识符设置要显示的文本。
  • final void setText(CharSequence text)设置要显示的文本。
  • void setText(CharSequence text, TextView.BufferType type)设置要显示的文本和TextView.BufferType。
  • final void setText(int resid, TextView.BufferType type)使用字符串资源标识符和TextView.BufferType设置要显示的文本。
  • final void setText(char[] text, int start, int len)将TextView设置为显示指定char数组的指定切片。

您正在尝试使用不受支持的setText(int,int)。你应该做些像textView2.setText(x1+" "+y1);这样的事情

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43198787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档