我已经尝试了很多方法来展示我想要的Toast(在DrawView类上),我真的不知道在哪里运行。
显然这很简单,但我错过了一些东西。我已经看过其他相关帖子,但没有成功。
有什么想法吗?
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);
}
}
DrawView类:
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;
}
}
答案 0 :(得分:4)
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;
}}
答案 1 :(得分:1)
按如下方式更改代码..
Context _context;
public DrawView(MyActivity myActivity,Context context) {
super(myActivity);
_context=context;
setBackgroundColor(Color.WHITE);
paint.setColor(Color.BLACK);
}
并使用Toast
_context
Toast.makeText(_context,"I'm a Toast!",Toast.LENGTH_SHORT).show();
将当前Context
作为参数传递给类..
答案 2 :(得分:1)
Toast not apper不是触摸事件问题时与您有关的上下文参考问题。您应该像这样更改 DrawView类
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;
}
}
和您的活动类一样
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
希望这会对你有所帮助
答案 3 :(得分:0)
您可以尝试更改代码:
Toast.makeText(getContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();
为:
Toast.makeText(getApplicationContext(),"I'm a Toast!",Toast.LENGTH_SHORT).show();
答案 4 :(得分:0)
您正在将“活动使用”作为您的上下文传递。