상세 컨텐츠

본문 제목

안드로이드 제공 searchable 이용해서 검색창 넣기

스마트기기개발관련/안드로이드 개발

by AlrepondTech 2011. 6. 20. 15:39

본문

반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

searchable 을 이용한 예제가 많지 않아서 잘 설명할수는 없지만 적어본다.


1 - //필요한 스티링 테이블 xml

안드로이드 프로젝트 만들때 만들어지는 value 폴더에 strings.xml 을 수정하면 된다.

<?xml version="1.0" encoding="utf-8"?>
<resources>

...

<!-- 필요 스트링 테이블 값을 직접 넣어주어두 되지만 되도록 예제 구성을 참조했다. -->

 <!-- The label for use as a searchable item -->
    <string name="search_label">Dictionary</string>
 <!-- The hint text that appears in the search box. -->
    <string name="search_hint">Search the dictionary</string>
 <!-- The description that will show up in the search settings for this source.  -->
    <string name="settings_description">Definitions of words</string>
  <!-- The menu entry that invokes search. -->
    <string name="menu_search">Search</string>

</resources>



2 - 안드로이드 res/xml 폴더를 만든다

//file name: searchable.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- The attributes below configure the Android search box appearance
     and the search suggestions settings.
     See the Developer Guide for more information
     http://developer.android.com/guide/topics/search/
 -->
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/search_label"
        android:hint="@string/search_hint"
        android:searchSettingsDescription="@string/settings_description"
        android:searchSuggestIntentAction="android.intent.action.VIEW"
        android:searchSuggestIntentData="content://com.home.psj.search.CSearchView"
        android:searchSuggestSelection=" ?"
        android:searchSuggestThreshold="1"
        android:includeInGlobalSearch="true"
        >
 </searchable>

3 -매니페스트에 등록한다. // 각기 안드로이드 매니페스트 마다 이름이 틀릴수도 있다.//

안드로이드 프로젝트 만들때 만들어지는 파일이다.!!

//file androidManifest.xml

?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 
..... (중간 기본 xml 생략)
       
<!-- 주석: activity android:name 에 자기가 searchable을 넣을 뷰 경로를 넣는다. -->

<activity android:name=".search.CSearchView"
                  android:label="@string/app_name" android:debuggable="true">
      
              <!-- search { -->
              <!-- Receives the search request. -->
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
                <!-- No category needed, because the Intent will specify this class component-->
               <!--   <category android:name="android.intent.category.DEFAULT" /> -->
              
            </intent-filter>

            <!-- Points to searchable meta data. -->
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable" />
            <!-- }search -->

        </activity>

..... (중간 기본 xml 생략) <!-- 아랫부분 xml 도 넣어줄 필요없는 기본 xml 이다, 색깔부분만 추가 -->
       
          <!-- Points to searchable activity so the whole app can invoke search. -->
        <meta-data android:name="android.app.default_searchable"
                   android:value=".search.CSearchView" />
       

    </application>
   
    <receiver android:name=".network.CConnReceiver" android:enabled="true" android:priority="0">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

    <uses-sdk android:minSdkVersion="8" />
   
    <uses-permission android:name="android.permission.INTERNET">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
    </uses-permission>
    </uses-permission>
   
</manifest>



4 - // 실행 소스 코드

소스코드 경로 - 아까 위에서 경로 지정했던 서치 뷰어이다.

//경로 - com.home.psj.search.CSearchView

package com.home.psj.search;

import java.util.ArrayList;

public class CSearchView extends Activity
{
    ..... (기본 코드들)
 
    void ExFunction()
    { 
      

   // searchable 작동!!
         onSearchRequested();

    }

}




5 - // 데이터 입력 이벤트

2 - //안드로이드 res/xml 폴더를 만든다.// 여기에 코드android:searchSuggestIntentData="content://com.home.psj.search.CSearchView"
부분이 이벤트를 등록하고 아래 코드,


package com.home.psj.search;

import java.util.ArrayList;

public class CSearchView extends Activity
{
    ..... (기본 코드들)

  public void onCreate(Bundle savedInstanceState) {
  
     

Intent intent = getIntent();
    
      if (Intent.ACTION_VIEW.equals(intent.getAction())) {
       
          finish();
      } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        
          String query = intent.getStringExtra(SearchManager.QUERY);
         
          finish();
      }

  }

}

"query" 부분에서 이벤트가 발생하면 에디터에 입력했던 값이 들어있다.



=================================

=================================

=================================

 

 

반응형


관련글 더보기

댓글 영역