2017-06-19 55 views
-1

在我發佈之前,我嘗試了所有可能的解決方案。最後我陷入了困境,沒有運氣。我只想在搜索活動中顯示查詢字符串。任何幫助?提前致謝。從未調用過Android搜索活動

的AndroidManifest.xml:

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/my_app" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <meta-data 
     android:name="android.app.default_searchable" 
     android:value="com.mycompany.mygoodapp.SearchResultsActivity"/> 

    <activity 
     android:name=".MainActivity" 
     android:configChanges="orientation" 
     android:label="@string/my_app" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="stateHidden|adjustPan"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".SearchResultsActivity" 
     android:label="@string/my_app" 
     android:launchMode="singleTop"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

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

    </activity> 
</application> 

SearchResultsActivity實現如下:

public class SearchResultsActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_results); 

    DisplayToast("Here ..."); 
    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 

    setIntent(intent); 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     DisplayToast(query); 

    } 

} 

public void DisplayToast(String s) { 

    Toast.makeText(SearchResultsActivity.this, 
      s, Toast.LENGTH_LONG).show(); 
    } 

} 

searchable.xml:

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

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

MainActivity:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 

    getMenuInflater().inflate(R.menu.menu_main, menu); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     SearchManager searchManager = 
       (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = 
       (SearchView) menu.findItem(R.id.search).getActionView(); 
     ComponentName cn = new ComponentName("com.mycompany.mygoodapp", "com.mycompany.mygoodapp.SearchResultsActivity"); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); 

     searchView.setIconifiedByDefault(true); 

    } 

return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    /// 
    else if(id==R.id.search) 
    { 
     onSearchRequested(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

主菜單佈局搜索菜單項:

<item 
    android:id="@+id/search" 
    android:title="@string/search_title" 
    android:icon="@android:drawable/ic_menu_search" 
    app:showAsAction="always" 
    app:actionViewClass="android.support.v7.widget.SearchView"> 

+1

「我嘗試了所有可能的解決辦法張貼在這裏」你嘗試過哪些解決方案? –

+0

嗨dask。只需查看本教程http://blog.nkdroidsolutions.com/android-searchview-in-listview-example-tutorial/並自己嘗試一下。 – RameshJaga

+0

@dask檢查我的答案 –

回答

0

您可以通過以下兩種方式

1路

菜單/ options_menu.xml

搜索瞭解創建活動
<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/action_search" 
    android:icon="@drawable/disha" 
    app:showAsAction="collapseActionView|always" 
    android:title="search" 
    app:actionViewClass="android.support.v7.widget.SearchView"/> 
</menu> 
爲您的活動

Java代碼

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

// Associate searchable configuration with the SearchView 
SearchManager searchManager = 
     (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
SearchView searchView = 
     (SearchView) menu.findItem(R.id.action_search).getActionView(); 
searchView.setSearchableInfo(
     searchManager.getSearchableInfo(getComponentName())); 

return true; 
} 

在manifiest文件中添加此

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

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="search hint" /> 

使用自定義

2路動作條 或者你也可以試試這個

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

getSupportActionBar().setCustomView(R.layout.search_layout); 

search = (EditText) getSupportActionBar().getCustomView(). 
     findViewById(R.id.searchfield); 

search.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, 
            KeyEvent event) { 
     return true; 
    } 
}); 

getSupportActionBar().setDisplayOptions(getSupportActionBar().DISPLAY_SHOW_CUSTOM 
     | getSupportActionBar().DISPLAY_SHOW_HOME); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

比創建一個自定義actiob欄佈局這樣

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/searchfield" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/custom_search_edit" 
android:hint="@string/search_hint" 
android:inputType="textFilter" 
android:textColor="@color/colorBlack" 
android:textColorHint="@color/colorAccent" 
android:textSize="15sp" /> 

試試這個,問我任何查詢的案例