使用XML onClick字段在DrawingView中调用函数

时间:2017-02-06 13:13:22

标签: java android xml android-studio

我正在android studio中开发一个应用程序。我目前有两个部分,MainActivity和DrawingView。在DrawingView中既是Canvas又是Button。

<com.jerem.simpledraw.DrawingView
    android:id="@+id/DrawingView"
    android:layout_width="wrap_content"
    android:layout_height="554dp"
    android:background="#ffffff"
    android:paddingBottom="40dp"
    android:paddingLeft="20dp"
    app:exampleColor="#33b5e5"
    app:exampleDimension="24sp"
    app:exampleDrawable="@android:drawable/ic_menu_add"
    app:exampleString="Hello, DrawingView" />

<Button
    android:text="Refresh"
    android:layout_height="wrap_content"
    android:id="@+id/buttonRefresh"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:onClick="refreshCallback"/>

以上是绘图视图的布局文件。我希望按钮在按下时调用方法&#34; refreshCallback&#34;,它在DrawingView类中。

However, this warns me that it cannot find the method in the MainActivity.

我想在DrawingView中调用一个方法,而不是在Main Activity中。我该如何解决这个问题?

编辑:我正在更新以包含整个DrawingView类。我想刷新页面。

package com.jerem.simpledraw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

public class DrawingView extends View
{
//Constant
private int Colour = 0xFF000000;
private int screen_width;
private int screen_height;

//Drawing Var
private Canvas drawCanvas;
private Bitmap canvasBitmap;
private Path drawPath;
private Paint drawPaint;
private Paint canvasPaint;

public DrawingView(Context context, AttributeSet attributeSet){
    super(context, attributeSet);
    setupDrawing();
    setupUI();
}

private void setupUI() {

}

private void setupDrawing() {
    drawPath = new Path();
    drawPaint = new Paint();

    drawPaint.setColor(Colour);

    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(20);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);

    canvasPaint = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

private void handelAction(int action, int x, int y){
    switch(action) {
        case 0:
            drawPath.moveTo(x, y);
            break;
        case 2:
            drawPath.lineTo(x, y);
            break;
        case 1:
            drawCanvas.drawPath(drawPath, drawPaint);
            break;
    }
    {
        invalidate();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event){
    float x = event.getX();
    float y = event.getY();

    System.out.println(event.getAction());
    handelAction(event.getAction(), (int) x, (int) y);
    return true;
}

public void refreshCallback(View v){
    System.out.println("Refresh Command Sent");
    this.setupDrawing();
}

}

3 个答案:

答案 0 :(得分:0)

您只能通过xml onClick调用活动的方法。因此,您需要MainActivity中的方法将函数调用传递给兄弟视图。如果您想知道为什么会这样,请考虑以下问题:运行时应该如何决定当前哪个视图&#39;当用户点击按钮时应该调用方法吗?

编辑:今天我了解到事实上可以通过onClick引用活动类中的静态方法。如果您打算使用活动的非静态方法,则不应将该方法声明为静态方法(反之亦然,我想)。

答案 1 :(得分:0)

事实是onclick只是试图找到当前类正在使用它的方法。如果需要,可以执行此操作,请按照以下步骤操作: -

MainActivity:

public void callRefreshCallback(View v){
   (new DesignView()).refreshCallback(v);
}

DesignView:

public static void refreshCallback(View v){
//dosomething.
}

MainActivity.xml:

android:onClick="callRefreshCallback" 

答案 2 :(得分:0)

在Activity类中尝试此代码。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_drawing_view);
        DrawingView drawingView = (DrawingView) findViewById(R.id.DrawingView);
        Button buttonRefresh = (Button) findViewById(R.id.buttonRefresh);
        buttonRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawingView.refreshCallback(view);
            }
        });
}