【问题标题】:Text Watcher not showing the toast in androidText Watcher 未在 android 中显示吐司
【发布时间】:2013-10-14 22:26:26
【问题描述】:

我正在使用 editname.addTextChangedListener(watcher); 在 android 中为 editText 调用异步任务,在我在编辑文本中键入字符后,它在 android 中不起作用。

 private final TextWatcher watcher=new TextWatcher(){
            @Override
            public void afterTextChanged(Editable s){
            if(s.length()<=4){
            Toast.makeText(getApplicationContext(), "type above 4 characters",  Toast.LENGTH_SHORT).show();
            }
            else{
                String url="http://10.0.2.2/chat/q.php?username="+s.toString();
                 if(task != null && (task.getStatus() == AsyncTask.Status.FINISHED)){
                task.execute(new String[]  {url});
                 }

            }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {


            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {


            }

        };

我的问题是 toast 不适用于我的代码。请帮忙

【问题讨论】:

  • Toast.makeText(YourActivity.this, "输入超过 4 个字符", Toast.LENGTH_SHORT).show();试试这样。
  • 尝试在 onTextChanged 中添加你的代码

标签: android android-edittext android-toast


【解决方案1】:

试试这个-

Toast.makeText(YourActivity.this, "type above 4 characters", Toast.LENGTH_SHORT).show();

YourActivity 替换为您的Activity 的名称。

【讨论】:

    【解决方案2】:

    Toast 中传递除getApplicationContext() 之外的活动上下文。

     Toast.makeText(Mainactivity.this, "type above 4 characters",  Toast.LENGTH_SHORT).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 2016-06-16
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多