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

标签 android toast

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

相关文章:

javascript - Android Emulator 浏览器是否适合并可靠地测试 HTML/Javascript 应用程序?

服务中的android自定义 toast

ios - 较小尺寸的 uiviewcontroller

php - 用于检查 MYSql 中现有行的 Android PHP 代码不起作用

android - 立即 toast

php - 值总是返回 0,它应该返回插入的行数

android - 如何创建半椭圆形(弯曲一条线)

安卓应用 : logging on the device

android - ViewPager 不显示工具栏和选项卡

android - 如何随机改变 Sprite 的颜色