本文介绍了自动完成延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的应用设置自动完成功能.

我已经了解了AutoCompleteTextView操作,但是我想动态地修改android自动完成功能所使用的String[].

我想做的事情:调用一个PHP页面,该页面将给我一个将在AutoCompleteTextView中使用的String[],但是我只想在最后一个键之后至少500毫秒按下一个键./p>


好的,我错了.我想让我的asynctask运行,如果在最后一次按下后500毫秒内没有按下任何键(您知道,通过对每个键入的字符调用一个请求来避免服务器收费过高).


这就是我想做的事:

zipBox.setAdapter(myAdapter); //zipBox is an AutoCompleteTextView
zipBox.addTextChangedListener(new TextWatcher(){

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

        d = new Date();
        GetAutocompletion ac = new GetAutocompletion();
        ac.execute(zipBox.getText().toString());
    }
    // Other methods to implement
});


private class GetAutocompletion extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params){

        //adapter.notifyDataSetChanged();

        try{
             wait(500);
        } catch(InterruptedException e){}

        Date d1 = new Date();

        if(d1.getTime() - d.getTime() > 500){
            String res = "";

            try{

                URLConnection conn = new URL("myUrl.php?term=" + params[0]).openConnection();
                InputStream in = conn.getInputStream();
                Scanner s = new Scanner(in).useDelimiter("\\A");

                while (s.hasNext())
                    res += s.next();

                s.close();
                in.close();

            } catch(Exception e){ Log.d("eduine", "error during autocompletion receive"); }

            return json;
        } else return null;
    }

    @Override
    protected void onPostExecute(String result){

        super.onPostExecute(result);

        Log.d("eduine", "json result : " + result);
    }

}

您怎么看?反正我可以使用Timer类吗?

解决方案

我将保留一个长名称lastPress作为TextWatcher上的一个字段.当您按一个键时,设置lastPress = System.currentTimeMillis().然后只需将整个onTextChanged包裹在条件为if(System.currentTimeMillis() - lastPress>500)的if中,并在该if中再次设置lastPress.


new TextWatcher(){
    long lastPress = 0l;
    @Override
    public void onTextChanged(CharSequence s,
             int start, int before, int count){
        if(System.currentTimeMillis() - lastpress > 500){
            lastPress= System.currentTimeMillis();
            GetAutocompletion ac = new GetAutocompletion();
            ac.execute(zipBox.getText().toString());
        }
    }
    // Other methods to implement
}

I've got to set an autocompletion for my application.

I've already understood the AutoCompleteTextView operation, but I'd like to dynamically modify the String[] used by android autocompletion.

What I wanna do : call a PHP page which will give me a String[] that I'll use in my AutoCompleteTextView, but i wanna do that ONLY if a key was pressed at least 500 milliseconds after the last one.


EDIT :

Okay, I was wrong. I want to have my asynctask running if NO KEY is pressed in the 500 milliseconds after the last press (you know, avoiding overcharging our servers by calling a request on every character typed in.)


Here is how I think I'll do :

zipBox.setAdapter(myAdapter); //zipBox is an AutoCompleteTextView
zipBox.addTextChangedListener(new TextWatcher(){

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

        d = new Date();
        GetAutocompletion ac = new GetAutocompletion();
        ac.execute(zipBox.getText().toString());
    }
    // Other methods to implement
});


private class GetAutocompletion extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params){

        //adapter.notifyDataSetChanged();

        try{
             wait(500);
        } catch(InterruptedException e){}

        Date d1 = new Date();

        if(d1.getTime() - d.getTime() > 500){
            String res = "";

            try{

                URLConnection conn = new URL("myUrl.php?term=" + params[0]).openConnection();
                InputStream in = conn.getInputStream();
                Scanner s = new Scanner(in).useDelimiter("\\A");

                while (s.hasNext())
                    res += s.next();

                s.close();
                in.close();

            } catch(Exception e){ Log.d("eduine", "error during autocompletion receive"); }

            return json;
        } else return null;
    }

    @Override
    protected void onPostExecute(String result){

        super.onPostExecute(result);

        Log.d("eduine", "json result : " + result);
    }

}

What do you think? Is there anyway I could use Timer class?

解决方案

I'd keep a long named lastPress as a field on my TextWatcher. When you press a key, set lastPress = System.currentTimeMillis(). Then just wrap your entire onTextChanged in a if with condition if(System.currentTimeMillis() - lastPress>500) and set lastPress again in that if.


new TextWatcher(){
    long lastPress = 0l;
    @Override
    public void onTextChanged(CharSequence s,
             int start, int before, int count){
        if(System.currentTimeMillis() - lastpress > 500){
            lastPress= System.currentTimeMillis();
            GetAutocompletion ac = new GetAutocompletion();
            ac.execute(zipBox.getText().toString());
        }
    }
    // Other methods to implement
}

这篇关于自动完成延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:34