单击搜索后,为searchview打开新活动

时间:2019-12-18 10:19:23

标签: android android-fragments kotlin searchview android-search

我想在search icon上单击mainactivity时打开一个新活动。这个名为SearchableActivity的新活动将处理listview中的所有相关搜索结果。我已按照本post中所述的步骤进行操作。

这是我目前拥有的:

Main activity display

View after search icon is clicked

View after enter is pressed on text query into the search

我想在用户输入SearchableActivity时打开SearchView

这是我的代码:

AndroidManifest.xml

     <activity
            android:name=".MainActivity"
            android:launchMode="singleTop">
            <!-- meta tag points to the activity which displays the results -->
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchableActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchableActivity"
            android:parentActivityName=".MainActivity">
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
            <!-- meta tag and intent filter go into results activity -->
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
        </activity>

MainActivity

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.options_menu, menu)
        val manager = getSystemService(SEARCH_SERVICE) as SearchManager
        (menu?.findItem(R.id.search)?.actionView as SearchView).apply {
            setSearchableInfo(manager.getSearchableInfo(ComponentName(this.context, SearchableActivity::class.java)))

        }
        return true
    }

SearchableActivity

class SearchableActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_searchable)
        Toast.makeText(this@SearchableActivity, "SA created", Toast.LENGTH_LONG).show()
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (intent !== null)
            handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        if (Intent.ACTION_SEARCH == intent.action) {
            val query = intent.getStringExtra(SearchManager.QUERY)
            Toast.makeText(this@SearchableActivity, "QUERY: $query", Toast.LENGTH_LONG).show()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

有一种处理选项菜单项单击的方法,称为

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if(item.itemId == R.id.myItemId){
        startActivity(Intent(applicationContext, searchAcctivity::class.java))
    }
    return super.onOptionsItemSelected(item)
}

此代码是用kotlin编写的,在Java中应该没什么不同,只需按ctrl + o,它应该打开一个搜索“ onOptionsItemSelected”的替代方法列表

答案 1 :(得分:0)

我不确定您要做什么,但是您可以简单地将Search查询传递给活动创建时的意图。

首先,将随行对象添加到您的SearchableActivity中:

  companion object {
        private const val EXTRA_QUERY = "SEARCH_QUERY"

        fun newIntent(context: Context?, query: String): Intent {
            return Intent(context, SearchableActivity::class.java)
                .putExtra(EXTRA_QUERY, query)
        }
    }

我的理解是,您想使用MainActivity的搜索查询打开一个新活动,接下来是处理意图:

         private fun handleIntent(intent: Intent) {
                if (Intent.ACTION_SEARCH == intent.action) {
                    val query = intent.getStringExtra(EXTRA_QUERY)
                    Toast.makeText(this@SearchableActivity, "QUERY: $query", Toast.LENGTH_LONG).show()

            SearchableActivity.newIntent(this, query)            
    }
  }

.. et voila。