gpt4 book ai didi

android - 如何在 android 的 edittext onclick 监听器中写入 toast 消息?

转载 作者:行者123 更新时间:2023-11-29 21:10:43 25 4
gpt4 key购买 nike

我是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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com