【问题标题】:How to get a button to work with view in android [closed]如何让按钮在android中使用视图[关闭]
【发布时间】:2014-07-15 19:49:26
【问题描述】:

我一直在尝试向这个绘图应用程序添加擦除功能。我本来打算在你移动手指的地方简单地画白色。出于某种原因,我无法让按钮工作。如果我能让这个按钮工作,我将能够添加更多颜色等。 (我没有对 Mainactivity 做过任何事情)

package com.example.draw;

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

public class MainDrawingView extends View
{
private Paint paint = new Paint();
private boolean erase;
private Path path = new Path();
Button aButton;

public MainDrawingView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(5f);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);

    aButton = (Button) this.findViewById(R.id.button1);
    aButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            erase = !erase;
        }
    });
}

@Override
protected void onDraw(Canvas canvas)
{
    canvas.drawPath(path, paint);
}

public boolean onTouchEvent(MotionEvent event)
{
    if(erase) paint.setColor(Color.WHITE);
    else paint.setColor(Color.BLACK);

    // Get the coordinates of the touch event.
    float eventX = event.getX();
    float eventY = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Set a new starting point
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            // Connect the points
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    // Makes our view repaint and call onDraw

    invalidate();

    return true;
}
}
Activity_main<

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff"

tools:context="com.example.draw.FullscreenActivity">



<!-- This is the view on which we will draw. -->

<view

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    class="com.example.draw.MainDrawingView"
    android:id="@+id/single_touch_view"

    android:layout_gravity="left|top"
    android:background="#ffffff" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</FrameLayout>

【问题讨论】:

  • 如果我删除 aButton = (Button) this.findViewById(R.id.button1); aButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { erase = !erase; } });它会正常工作,但按钮不会工作(显然)

标签: java android button view draw


【解决方案1】:

您的问题似乎是您使用了错误的类型。您的 erase 变量的类型为 Paint

private Paint erase = new Paint();

但在您的代码中,您将其视为代表booleanint

if(erase != 0) { paint.setColor(Color.BLACK); }
if(erase == 0) { paint.setColor(Color.WHITE); }
...
erase = 1;

其次,你从来没有真正为你的按钮设置onClickListener。此外,设置颜色的逻辑应该全部移到您的onTouchonClick 方法中。你真的应该有:

public class MainDrawingView extends View
{
    private Paint paint = new Paint();
    private boolean erase;
    private Path path = new Path();
    Button aButton;

    public MainDrawingView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5f);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

        aButton = (Button) this.findViewById(R.id.button1);
        aButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                erase = !erase;
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawPath(path, paint);
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        if(erase) paint.setColor(Color.WHITE);
        else paint.setColor(Color.BLACK);

        // Get the coordinates of the touch event.
        float eventX = event.getX();
        float eventY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Set a new starting point
                path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                // Connect the points
                path.lineTo(eventX, eventY);
                break;
            default:
                return false;
        }

        // Makes our view repaint and call onDraw

        invalidate();

        return true;
    }
}

【讨论】:

  • 它几乎工作了,但 Eclipse 说在公共类 MainDrawingView 扩展视图实现 onClick;我需要在onclick上创建一个界面。我该怎么做?
  • 抱歉,您可以删除它。我会更新答案
  • 它仍然无法正常工作。我将添加可能不适用于按下按钮的activity_main?使用屏幕上的按钮可以正常工作。
【解决方案2】:

您的视图可能不是使用以下构造函数创建的

Context context, AttributeSet attrs

它可能正在使用您尚未覆盖的不同默认构造函数。然后什么都不会设置,你也不会得到任何你期望从你的听众那里得到的点击行为。

【讨论】:

  • 我该如何检查呢??
  • @user3609407 如果要删除当前的构造函数,Eclipse 应该显示一个红色框并提供建议的包含。其中一个或几个可能是您需要的,具体取决于您如何称呼它。这是一种快速的方法,但另一个默认设置是仅使用您可以覆盖的 Context。
【解决方案3】:

您似乎没有设置 onCreate(Bundle savedInstances) 方法来正确添加侦听器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多