Android studio 如何在画布上单击imagebutton以及如何使用intent方法

Android studio 如何在画布上单击imagebutton以及如何使用intent方法,android-studio,Android Studio,这真的很令人沮丧,因为我仍在尝试为我的应用程序做一些事情来完成它。首先,我似乎无法将startActivity从主活动转到第二个活动,我确实创建了一个名为PaintView.class的新的第二个活动,并且已经看到了大多数使用下面代码的教程,但无论我使用OnClickListener()做什么,它都无法工作,也无法将用户发送到第二个活动。我的清单xml看起来也不错,但我不想更改它,因为我担心可能会在那里塞满东西 我的第二个活动类中也有一个名为clear_canvas id的imagebutton

这真的很令人沮丧,因为我仍在尝试为我的应用程序做一些事情来完成它。首先,我似乎无法将startActivity从主活动转到第二个活动,我确实创建了一个名为PaintView.class的新的第二个活动,并且已经看到了大多数使用下面代码的教程,但无论我使用OnClickListener()做什么,它都无法工作,也无法将用户发送到第二个活动。我的清单xml看起来也不错,但我不想更改它,因为我担心可能会在那里塞满东西

我的第二个活动类中也有一个名为clear_canvas id的imagebutton,但我似乎无法让它单击并响应。如果有人能在这里帮助我,我将不胜感激,因为我发布了一些帖子,但没有太多帮助……我正在尝试添加一个要绘制的背景图像,但似乎无法使我的imagebutton正常工作,因为我无法使用onclicklister()单击它。谢谢

这是我的主要活动代码

输入代码hepublic class main活动扩展AppCompative活动{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


//    PaintView pv = new PaintView(this);

    setContentView(R.layout.activity_main);


}

 public void practice (View v) {
     startActivity(new Intent(MainActivity.this, PaintView.class));
 }



}
这是我的PaintView课程:

enter code public class PaintView extends View{

LayoutParams params;
Path path = new Path();
Paint br = new Paint();
Bitmap background;
Bitmap background2;

public PaintView(Context context) {

    super(context);
    background = BitmapFactory.decodeResource(getResources(), R.drawable.worksheetblank2);
    background2 = BitmapFactory.decodeResource(getResources(), R.drawable.clear);

    int maxHeight = 2000;
    int maxWidth = 1000;
    float scale = Math.min(((float) maxHeight / background.getWidth()), ((float) maxWidth / background.getHeight()));

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    background = Bitmap.createBitmap(background, 0, 0, background.getWidth(), background.getHeight(), matrix, true);
    background2 = Bitmap.createBitmap(background2, 0, 0, background2.getWidth(), background2.getHeight(), matrix, true);



    br.setColor(Color.GREEN);
    br.setStyle(Paint.Style.STROKE);
    br.setStrokeJoin(Paint.Join.ROUND);
    br.setStrokeWidth(10f);

    params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

}


@Override
public boolean onTouchEvent(MotionEvent event) {

    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            path.moveTo(x, y);
            return true;

        case MotionEvent.ACTION_MOVE:
            path.lineTo(x, y);
            break;
    }
    postInvalidate();
    return false;
}

//@Override
protected void onDraw(Canvas canvas) {
    // Order of code matters here in order to draw on the background

    canvas.drawBitmap(background, 50, 0, null);
    canvas.drawBitmap(background2, 0, 50, null);
    canvas.drawPath(path, br);


}
}

这是我的清单文件

enter code<?xml version="1.0" encoding="utf-8"?>
输入代码


android:name=“.Paint”
android:label=“@string/title\u activity\u paint”
android:theme=“@style/AppTheme.NoActionBar”/>
这里

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".PaintView"></activity>


    android:name=".Paint"
    android:label="@string/title_activity_paint"
    android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>