根据输入时间间隔判断EditText是否输入完成

在android开发中经常会需要判断输入框的输入完成情况

根据个人经验总结出3条判断Editext输入完成

1添加text监听 在

afterTextChanged
事件中延时2000ms根据延时前和延时后的时间对比判断在2000ms内是否有再次输入情况
addTextChangedListener
 Boxname.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(final Editable s) {
      mill = System.currentTimeMillis();
            Log.i("+++++"+i, mill+""+System.currentTimeMillis());
            Handler handler = new Handler();
            handler.postDelayed(new splashhandler(), 2000);
            i++;
        }
    });
}
class splashhandler implements Runnable

{
    public void run() {

      System.currentTimeMillis();
        Log.i("+++++"+i, mill+""+System.currentTimeMillis());
       long ss= (System.currentTimeMillis())-mill;
        Log.i("*****+"+i, (System.currentTimeMillis())-mill+"");
        if(ss>=2000)//
{
            Log.i("+++++"+i, mill+"成功"+System.currentTimeMillis());//在2000ms内没有输入

        }
    }
}
2进行焦点监听 判断焦点是否离开(局限于有多个输入框否者焦点无法离开,而1没有限制)

et_warename.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View wrea_v, boolean hasFocus) {


        if (!hasFocus) {//判断焦点是否离开
            Map, String> params = new HashMap<>();
            String st = et_warename.getText().toString();
            params.put("areacode", st);

            OkHttpClientManager.postAsyn(confing.path + confing.queryareaname, params, new OkHttpClientManager.ResultCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(queryareanameModel name) {
                    et_warename.setText(name.getAreaname());
                    et_warecode.setText(name.getAreacode());
                    Wera_Focus = false;
                }
            });
        } else {
            Wera_Focus = true;
        }
    }


});

3 与1差不多,只是判断条件不同而已,个人推荐用3

     Boxname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(final Editable s) {
                mill = System.currentTimeMillis();
                if (delayRun != null) {
                    //每次editText有变化的时候,则移除上次发出的延迟线程
                    handler.removeCallbacks(delayRun);
                }
//                editString = s.toString();

                //延迟800ms,如果不再输入字符,则执行该线程的run方法
                if (!TextUtils.isEmpty(Boxname.getText())) {
                    if (!ValidateHelper.isChineseString(Boxname.getText()+"")) {
                        handler.postDelayed(delayRun, 2000);
                    }
                }

            }


        });
    }

    private Handler handler = new Handler();

    /**
     * 延迟线程,看是否还有下一个字符输入
     */
    private Runnable delayRun = new Runnable() {

        @Override
        public void run() {
            //在这里调用服务器的接口,获取数据
//            getSearchResult(editString, "all", 1, "true");
            queryitem();
        }
    };


你可能感兴趣的:(android)