2012-09-18 73 views
0

我有一個名爲searchOne的具有onKeyListener的編輯文本。我試圖完成的是獲取edittext(這是一個搜索查詢)中的值的方法,然後使用它來構建一個url。我想在用戶點擊搜索按鈕之前這樣做,以便儘可能少地使用我的onClick。我在的EditText框中輸入一個名稱,然後單擊鍵盤上的旁邊去下的EditText框和logcat會:onKeyListener存儲值的兩倍

searchURLOne包含:http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=christopher_guest

searchURLOne包含:http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=christopher_guesthttp://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=christopher_guest

,你可以查看searchURLOne的第二個和最終值包含字符串兩次。任何想法,爲什麼這是?

searchOne.setOnKeyListener(new View.OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      //make person search url1 
      final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne)); 
      searchURLOne = personSearchURLOne.toString(); 
      Log.d("search1", searchURLOne); 
      return false; 
     } 
    }); 

回答

0

我有某種原因onKeyListners同樣的問題被稱爲兩次

休耕這

setOnclickListner is initialized twice?

這裏是我的點擊listners發現被初始化兩次一個是按下事件,另一個是關鍵了事件,所以你需要

放在一個if條件像波紋管:

 searchOne.setOnKeyListener(new View.OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      //make person search url1 
      if(event.getAction() == KeyEvent.ACTION_DOWN) 
      { 
       final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne)); 
       searchURLOne = personSearchURLOne.toString(); 
       Log.d("search1", searchURLOne); 
       return true; 
      } 
      return false; 
     } 
    }); 
0

試試這段代碼片段

@Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 

     if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction()==0) 
     { 
      //your code 
      return true; 
     } 
     return false; 
    }