Android OnKeyUp事件无限循环

时间:2014-08-26 14:27:43

标签: android

我想在Asynctask中搜索一些内容,但是当用户按下Enter按钮时,onKeyUp事件会有一个无限循环。

这是我的代码:

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
                String searchObject = tvSearch.getText().toString();
                searchObject = searchObject.replaceAll(" ", "%20");
                AsyncSearchRegion asr = new AsyncSearchRegion(MapActivity.this);
                tvSearch.onEditorAction(EditorInfo.IME_ACTION_DONE);
                asr.execute(searchObject);
                return true;
            default:
                return super.onKeyUp(keyCode, event);
        }
    }

的AsyncTask:

@Override
protected ArrayList<Double> doInBackground(String... params) {
    this.region = params[0];
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+region+"&sensor=false";

    JSONObject obj = Utilitys.getJSONFromUrl(url);

    //Parse json data
    try {
        String resultStatus = obj.getString("status");
        if(resultStatus.equals("OK")){
            JSONObject locations = obj.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
            lat = locations.getDouble("lat");
            lot = locations.getDouble("lng");
        }
        coordArray.add(lat);
        coordArray.add(lot);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return coordArray;
}

1 个答案:

答案 0 :(得分:1)

我认为问题与此方法调用有关:

tvSearch.onEditorAction(EditorInfo.IME_ACTION_DONE);

我不知道你为什么要把这个事件发送到搜索EditText。如果您试图隐藏键盘,请使用:

    public static void hideKeyboard(Context context, EditText editText) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}