简体   繁体   中英

Android, java: enter search query in activity

I want a user to type a search query in a mainactivity , and then I'll save this query and do a search in another activity( searchable ). But now I only have code for when an item " search " pressed another activity appears and user's typing query there in a searchdialog . How should I make a typing in and opening a search dialog in my main activity, and then, after entering, go to searchable activity? Here's an existing code:

AndroidManifest.xml:

       <activity
        android:name=".MainActivity"
        android:screenOrientation="sensor" >
        <intent-filter
            android:clearTaskOnLaunch="true"
            android:label="@string/app_name"
            android:stateNotNeeded="true" >
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="Searchable"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

MainActivity.java:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.search:
        choise = 0;
        onSearchRequested();
        Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
        Intent intent_search = new Intent(this, Searchable.class);
        startActivity(intent_search);
        return true;
...

Searchable.java:

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

You can use OnQueryTextChange method of queryTextListener.

SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            return true;
        }
        @Override
        public boolean onQueryTextSubmit(String query) {

        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM