2011-04-19 8 views
11

내 application.now에서 검색보기를 사용하고 있습니다. searchview 텍스트 상자에 입력 된 텍스트를 가져 와서 다른 textview.if에 표시하고 싶습니다. 텍스트를 입력하고 버튼을 내가 same.but 내가 할 수있는 여분의 buttons.i을 사용하고 싶습니다 그냥 결과 key.plz 사람이 나를 도울 때 표시하고 싶습니다 .. (만약 어떤 사람이 내게 코드를 제공하는 청취자 PLZ suggeting)android에서 searchview의 이벤트를 얻는 방법

+0

... –

답변

32

는 SearchView

new SearchView.OnQueryTextListener() { 
    @Override 
    public boolean onQueryTextChange(String newText) { 
     // your text view here 
     textView.setText(newText); 
     return true; 
    } 

    @Override 
    public boolean onQueryTextSubmit(String query) { 
     textView.setText(query); 
     return true; 
    } 
} 
+1

:

이 RXAndroid를 얻을 수 REPO를 이눔 수있는 링크입니다 .... –

+2

잘 작동합니다. 먼저 searchview에 리스너를 추가해야했습니다. – Asim

+1

입력 된 문자열이 비어 있지 않으면 리스너가 아니거나 onQueryTextSubmit이 작동하지 않습니다. –

16

위의 대답은 좋은 그러나 당신이 당신의 검색보기 위해 액션 청취자를 설정할 필요가 실제로 완료하지의 setOnQueryTextListener를 사용해보십시오.

 SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); 
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String query) { 
       callSearch(query); 
       return true; 
      } 

      @Override 
      public boolean onQueryTextChange(String newText) { 
//    if (searchView.isExpanded() && TextUtils.isEmpty(newText)) { 
        callSearch(newText); 
//    } 
       return true; 
      } 

      public void callSearch(String query) { 
       //Do searching 
      } 

     }); 
5

: 당신은 OnQueryTextListener을하고 그 새로운 객체를 만들어 다음과 같은 컴팩트 한 당신의 검색보기 쿼리 텍스트 리스너로 사용하거나 사용하는 데 필요한 클래스를 구현하는 클래스를 만드는 두 가지 방법으로이 작업을 수행 할 수 있습니다 또한이 같은 제이크 와튼에 의해 RXAndroid 및 RxBinding 수행 할 수 있습니다 :

RxSearchView.queryTextChanges(searchView) 
      .debounce(500, TimeUnit.MILLISECONDS) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .subscribe(new Action1<CharSequence>() { 
       @Override 
       public void call(CharSequence charSequence) { 
        if(charSequence!=null){ 
         // Here you can get the text 
         System.out.println("SEARCH===" + charSequence.toString()); 
        } 
       } 
      }); 

코드는 500 밀리 초 일부 지연 텍스트의 변화를 관찰하기 위해 가입한다. 작업이 아니다 나는 그것을 이러지 시도 내가 많은 청취자를했지만, 그들 중 누구도 working.i DNT KNW D 이유를 r에하지 https://github.com/JakeWharton/RxBinding

관련 문제