Toast 未显示 - Android

Toast not showing - Android

我已经尝试了很多方法来显示我想要的 Toast(在 DrawView Class 上),我真的不知道该去哪里 运行。

显然这很简单,但我遗漏了一些东西。我看过其他相关帖子,但没有成功。

有什么想法吗?

import android.app.Activity;
import android.os.Bundle;


public class MyActivity extends Activity{

    DrawView drawView;

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

        drawView = new DrawView(this);
        setContentView(drawView);

    }

}

绘图视图Class:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


public class DrawView extends View implements View.OnTouchListener{

    Paint paint = new Paint();

    public DrawView(MyActivity myActivity) {
        super(myActivity);

        setBackgroundColor(Color.WHITE);
        paint.setColor(Color.BLACK);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawText("I'm text in a canvas!", 10, 10, paint);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN){

            Toast.makeText(getContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

        }

        return false;

    }

}
public class DrawView extends View  {

Paint paint = new Paint();
private Context context;

public DrawView(MainActivity context) {
    super(context);
    this.context = context;
    setBackgroundColor(Color.WHITE);
    paint.setColor(Color.BLACK);
}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawText("I'm text in a canvas!", 10, 10, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        Toast.makeText(context, "I'm a Toast!", Toast.LENGTH_SHORT).show();

    }

    return false;

}}

您可以尝试更改您的代码:

Toast.makeText(getContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

至:

Toast.makeText(getApplicationContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();

您正在传递 Activity 使用它作为您的上下文。

更改代码如下..

Context _context;
public DrawView(MyActivity myActivity,Context context) {
        super(myActivity);

        _context=context;
        setBackgroundColor(Color.WHITE);
        paint.setColor(Color.BLACK);

    }

并使用 _context

显示 Toast
Toast.makeText(_context,"I'm a Toast!",Toast.LENGTH_SHORT).show();

将当前 Context 也作为参数传递给 class..

Toast not apper 不是上下文引用问题,它与您有关的触摸事件问题。您应该像这样 DrawView class

public class DrawView extends View  {

Paint paint = new Paint();
Context _MyActivity;

public DrawView(Context myActivity) {
    super(myActivity);
    this._MyActivity = myActivity;
    setBackgroundColor(Color.GREEN);
    paint.setColor(Color.BLACK);

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.drawText("I'm text in a canvas!", 10, 10, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // do something
        Toast.makeText(_MyActivity, "I'm a Toast!", Toast.LENGTH_SHORT)
        .show();
        break;
    case MotionEvent.ACTION_MOVE:
        // do something
        break;
    case MotionEvent.ACTION_UP:
       //do something
        break;
}
return true;
}



}

你的 activity class 这样

public class MyActivity extends Activity implements OnTouchListener {

DrawView drawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     drawView = new DrawView(this);
        setContentView(drawView);
        drawView.setOnTouchListener(this);
}



@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}

}

您不想在 DrawView.class

中实现 OnTouchListener

希望对您有所帮助