首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android OnTouch报错

Android OnTouch报错
EN

Stack Overflow用户
提问于 2017-06-14 01:58:14
回答 0查看 33关注 0票数 0

首先:我们是Android开发的新手,我们有一个练习,我们不知道我们需要做什么。

我们需要将OnTouchListener添加到touchView,根据课程幻灯片,可以按如下所述完成此操作,但它不起作用:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity{

TouchView touchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    this.setContentView(ll);

    float x = 500;
    float y = 500;
    float size = 1000;

    touchView = new TouchView(x,y,size,this);
    touchView.setOnTouchListener(this);
    ll.addView(touchView);
}

public boolean onTouch(View view, MotionEvent motionEvent){
    float x = motionEvent.getX();
    float y = motionEvent.getY();

    return  true;
}

使用如下所示的touchView类:

代码语言:javascript
复制
public class TouchView extends View {

public float x,y,size;
Paint paint = new Paint();

public TouchView(float xcor, float ycor, float sizenum, Context context) {

    super(context);
    x = xcor;
    y = ycor;
    size = sizenum;
    paint.setAntiAlias(true);

}



@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    paint.setColor(Color.BLUE);

    canvas.drawCircle(x, y, size, paint);
}

public TouchView(Context context, AttributeSet attributeSet){

    super(context, attributeSet);

}
}

不幸的是,它不起作用,我们完全不知道如何让它起作用。有人能帮帮我们吗?

EN

回答

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

https://stackoverflow.com/questions/44528460

复制
相关文章

相似问题

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