ค้นหาดูใน ActionBarSherlock

ฉันใช้วิดเจ็ต SearchView ใน ActionbarSherlock ดังนี้:

ไฟล์.java

public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.main, menu);

   SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
   SearchView searchView = (SearchView) menu.findItem(R.id.MenuSearch).getActionView();
   searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
   searchView.setIconifiedByDefault(true);
   searchView.setSubmitButtonEnabled(true);

   return true;
}

ไฟล์.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/MenuSearch" android:title="@string/Bs"
       android:icon="@drawable/ic_action_search"
       android:showAsAction="always|withText"
       android:actionViewClass="com.actionbarsherlock.widget.SearchView" >
    </item>

</menu>

ในแอปพลิเคชันของฉัน ฉันจะแสดงไอคอนค้นหา และเลือกไอคอนที่จะเปิดช่องค้นหาสำหรับวิดเจ็ต แต่เมื่อฉันเขียนการค้นหาใดๆ ฉันไม่รู้วิธีโต้ตอบกับวิดเจ็ต SearchView เพื่อเปิดกิจกรรมใหม่และแสดงชุดผลลัพธ์

ด้วยคำสั่ง searchView.setSubmitButtonEnabled(true); ปรากฏไอคอนคล้ายกับ 'เล่น' และฉันคิดว่ามันเป็นอย่างนั้น แต่ฉันไม่รู้ว่าจะโต้ตอบกับมันอย่างไร

ใครสามารถช่วยได้บ้าง?


person KryNaC    schedule 10.10.2013    source แหล่งที่มา


คำตอบ (1)


เอกสารอย่างเป็นทางการของ Android ที่นี่ อธิบายวิธีเริ่มกิจกรรมใหม่เพื่อจัดการกับคำค้นหาและแสดงผลลัพธ์

ขั้นแรก คุณต้องสร้างการกำหนดค่าที่ค้นหาได้ (หากคุณยังไม่ได้สร้าง) ใน res/xml/searchable.xml ดังนี้:

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

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint" />

ตั้งค่าการกำหนดค่านี้สำหรับกิจกรรมของคุณ ซึ่งมี SearchView ในแถบการทำงาน เช่นนี้ (ในรายการ):

<activity ... >
    ...
    <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

</activity>

โดยพื้นฐานแล้ว คุณต้องมีกิจกรรมที่สามารถค้นหาได้ของคุณ (กิจกรรมใหม่ที่จะเริ่มหลังจากส่งข้อความค้นหา) เพื่อจัดการกับการกระทำ android.intent.action.SEARCH แจ้งมาโดยชัดแจ้งดังนี้

<activity android:name=".SearchResultsActivity" ... >
    ...
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    ...
</activity>

และกิจกรรมที่ค้นหาได้ของคุณควรมีลักษณะดังนี้:

public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        ...
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
        }
    }
    ...
}
person hendrix    schedule 10.10.2013