Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何创建一个可点击的按钮和一个可变形的按钮?_Android_View_Android Canvas - Fatal编程技术网

Android 如何创建一个可点击的按钮和一个可变形的按钮?

Android 如何创建一个可点击的按钮和一个可变形的按钮?,android,view,android-canvas,Android,View,Android Canvas,我试图在画布上绘制一个形状,并获取有关用户在该画布上执行的单击的信息 我创建了一个扩展Button类的类 class CustomDrawableButton extends Button { private final ShapeDrawable mDrawable; int x = 50; int y = 50; int width = 30; int height = 30; public CustomDrawableButton (C

我试图在画布上绘制一个形状,并获取有关用户在该画布上执行的单击的信息

我创建了一个扩展Button类的类

  class CustomDrawableButton extends Button {
    private final ShapeDrawable mDrawable;
    int x = 50;
    int y = 50;
    int width = 30;
    int height = 30;

    public CustomDrawableButton (Context context) {
       super(context);
       mDrawable = new ShapeDrawable (new OvalShape ());
       mDrawable.getPaint().setColor(Color.GREEN);
       mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
      mDrawable.draw(canvas);
    }
  }
然后,在一个也扩展了View的类中,我添加了实例化和侦听器:

  CustomDrawableButton mCustomDrawableButton = new CustomDrawableButton(getBaseContext());

  layout.addView(mCustomDrawableButton);

  mCustomDrawableButton.draw(canvas);

  mCustomDrawableButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      System.out.println("Yey clicked");
     Toast.makeText(view.getContext(), "Yey", Toast.LENGTH_LONG).show();
    }});
执行此操作时,无法检测图像/按钮上的点击。我已经读到ShapeDrawable对象不能“单击”,但我希望它能工作,因为我扩展了按钮(这是一个视图,是“可单击的”)

这可能吗?如果不是这样,你能告诉我一些方法来获取画布或视图上按下的屏幕坐标的信息吗


提前感谢。

经过一些搜索后,这里有一个教程,演示如何通过获取触摸屏位置来执行此操作

仍然不知道如何通过自动将触摸事件绑定到按钮来实现。如果有人知道怎么做,请说出来

谢谢