帖上一代码,用Toast来简单处理回调机制.但是Toast的makeText()方法老报错:
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Toast;public class MyButton extends Button { public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
//就是这里了 Toast toast=Toast.makeText(MyButton.this, "the callBack test", 5000);
toast.show();
return true;
}
}
报错信息:The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (MyButton, String, int)
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Toast;public class MyButton extends Button { public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
super.onKeyDown(keyCode, event);
//就是这里了 Toast toast=Toast.makeText(MyButton.this, "the callBack test", 5000);
toast.show();
return true;
}
}
报错信息:The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (MyButton, String, int)
super(context, attrs);
mContext = context;
}Toast toast=Toast.makeText(mContext, "the callBack test", 5000);
toast.show();
第三个参数也不正确,应该是
Toast.makeText(getContext(), "the callBack test", Toast.LENGTH_LONG).show();
第一个参数让你传递一个Context对象,你传递一个Button对象肯定不对了