我是android开发的初学者,
我在 edittext 中输入文本后需要得到提示信息 我使用了以下代码:
order_dicount_Edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String dis=order_dicount_Edit.getText().toString();
int disc=Integer.parseInt(dis);
if(disc>100)
{
Toast.makeText(this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();
}
}
我收到如下错误:
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
最佳答案
改变这个
Toast.makeText(this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();
到
Toast.makeText(ActivityName.this, "Discount should be less than 100",Toast.LENGTH_SHORT).show();
或
Toast.makeText(v.getContext(), "Discount should be less than 100",Toast.LENGTH_SHORT).show();
this
没有引用有效的上下文。它引用了 new View.OnClickListener()
,它是一个匿名内部类,它实现了接口(interface) OnClickListener
。
最好还是试一试
try
{
int disc=Integer.parseInt(dis);
}catch(NumberFormatException e)
{
e.printStackTrace();
}
关于android - 如何在 android 的 edittext onclick 监听器中写入 toast 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962736/