2016-12-14

【Android Studio】為 app 加入查詢 / 尋找 / 搜尋 功能 -- 查詢 & 結果 在同一個畫面內

延續上一篇筆記:【Android Studio】為 app 加入查詢 / 尋找 / 搜尋 功能

是將查詢結果顯示在 SearchResultActivity,如果我想將查詢結果顯示在 MainActivity 呢?

可以的,只要稍加修改,將部份程式碼從 SearchResultActivity 搬到 MainActivity 就行了

AndroidManifest.xml 內容:
  1.  
  2. ...
  3. ...
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11.  
  12. <!-- Points to searchable activity so the whole app can invoke search. -->
  13. <meta-data
  14. android:name="android.app.default_searchable"
  15. android:value=".MainActivity" />
  16.  
  17. <activity
  18. android:name=".MainActivity"
  19. android:launchMode="singleTop">
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN"/>
  22. <action android:name="android.intent.action.SEARCH" />
  23. <category android:name="android.intent.category.LAUNCHER"/>
  24. </intent-filter>
  25.  
  26. <meta-data
  27. android:name="android.app.searchable"
  28. android:resource="@xml/searchable"/>
  29. </activity>
  30.  
  31. </application>
  32.  

activity_main.xml 內容:
  1. <RelativeLayout
  2. ...
  3. ...>
  4. <TextView
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:text="search result"
  8. android:id="@+id/txt1"/>
  9. </RelativeLayout>

MainActivity 內容:
  1.  
  2. ...
  3. ...
  4.  
  5. import android.app.SearchManager;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.view.Menu;
  11. import android.view.MenuInflater;
  12. import android.support.v7.widget.SearchView;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends AppCompatActivity
  16. {
  17. TextView txt1;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. txt1 = (TextView) findViewById(R.id.txt1);
  26.  
  27. // 注意這一行指令
  28. handleIntent(getIntent());
  29. }
  30.  
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu)
  33. {
  34. MenuInflater inflater = getMenuInflater();
  35. inflater.inflate(R.menu.options_menu, menu);
  36.  
  37. // Associate searchable configuration with the SearchView
  38. SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  39. SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
  40. searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  41.  
  42. // 顯示完成鈕
  43. searchView.setSubmitButtonEnabled(true);
  44.  
  45. return true;
  46. }
  47.  
  48. @Override
  49. protected void onNewIntent(Intent intent)
  50. {
  51. handleIntent(intent);
  52. }
  53.  
  54.  
  55. private void handleIntent(Intent intent)
  56. {
  57. if (Intent.ACTION_SEARCH.equals(intent.getAction()))
  58. {
  59. String query = intent.getStringExtra(SearchManager.QUERY);
  60. txt1.setText("傳遞的查詢字串為 "+query.toString());
  61. }
  62. }
  63. }
  64.