意向下一个活动时,点击搜索小工具 [英] Intent to next activity when search widget is clicked

查看:145
本文介绍了意向下一个活动时,点击搜索小工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何实现这一目标?

我想补充我的应用程序栏搜索小部件。当它被点击时,将意图到B,并允许用户键入。相关结果将显示如下。

我试过很多方法,但是当我点击搜索小部件,将其更改为 EDITTEXT ,但仍保持在同一页面。我怎样才能使它意图到另一个活动时就切换到 EDITTEXT ?谢谢你。

活动A

  @覆盖
        公共布尔onCreateOptionsMenu(菜单菜单){
            MenuInflater吹气= getMenuInflater();
            inflater.inflate(R.menu.create_menu,菜单);
            //使用搜索查看搜索关联配置
            SearchManager的SearchManager的=
                    (SearchManager的)getSystemService(Context.SEARCH_SERVICE);
            搜索查看搜索查看=
                    (搜索查看)menu.findItem(R.id.action_search).getActionView();
            sea​​rchView.setSearchableInfo(
                    sea​​rchManager.getSearchableInfo(getComponentName()));
            返回true;
        }@覆盖
    公共布尔onOptionsItemSelected(菜单项项){        开关(item.getItemId()){            案例R.id.action_search:
                意向意图=新意图(A.this,B.class);
                startActivity(意向);
                返回true;

活动B

 公共类SearchResultActivity扩展ActionBarActivity {
        TextView的文本;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_result);
            文字=(TextView的)findViewById(R.id.textView1);
            handleIntent(getIntent());
        }
 @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.create_menu,菜单);
        SearchManager的SearchManager的=
                (SearchManager的)getSystemService(Context.SEARCH_SERVICE);
        搜索查看搜索查看=
                (搜索查看)menu.findItem(R.id.action_search).getActionView();
        sea​​rchView.setSearchableInfo(
                sea​​rchManager.getSearchableInfo(getComponentName()));        返回true;
    }
        @覆盖
        保护无效onNewIntent(意向意图){
            setIntent(意向);
            handleIntent(意向);
        }        私人无效handleIntent(意向意图){            如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
                查询字符串= intent.getStringExtra(SearchManager.QUERY);
                text.setText(查询);
                //使用查询来搜索
            }
        }
    }

这是我想达到的目标。

活动A

在这里输入的形象描述

当点击搜索图标,在活动B改为EDITTEXT和显示应用栏下面的相关结果。

活动B

在这里输入的形象描述


解决方案

请按照下列步骤操作:


  

first.xml


 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android><项目机器人:ID =@ + ID / M1
    机器人:标题=搜索
    机器人:showAsAction =总是
    机器人:图标=@机器人:可绘制/ ic_menu_search/>


  

MainActivityFirst.java


  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.first,菜单);
    返回super.onCreateOptionsMenu(菜单);
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    如果(item.getItemId()== R.id.m1){
        意图I =新意图(这一点,MainActivitySecond.class);
        startActivity(ⅰ);
        返回true;
    }
    返回super.onOptionsItemSelected(项目);
}


  

second.xml


 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android><项目
    机器人:ID =@ + ID /搜索
    机器人:图标=@机器人:可绘制/ ic_menu_search
    机器人:标题=搜索
    机器人:showAsAction =总是
    机器人:actionViewClass =android.widget.SearchView/>


  

MainActivitySecond.java


  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.second,菜单);
    搜索查看搜索查看=
    (搜索查看)menu.findItem(R.id.search).getActionView();
        sea​​rchView.setIconified(假);
        返回super.onCreateOptionsMenu(菜单);
    }

searchView.setIconified(假)的将显示在第二个活动扩大了搜索视图,在这里你可以搜索视图玩,建议等。
欲了解更多后续<一个href=\"http://developer.android.com/intl/pt-br/guide/topics/search/search-dialog.html#UsingSearchWidget\"相对=nofollow>创建搜索Android中接口

希望这将解决您的问题。

Can someone tell me how to achieve this ?

I wanted to add a search widget in my app bar. When it is clicked, it will intent to B and allow user to type. The related result will displayed below.

I've tried many method, but when I click the search widget, it change to editText, but still remains in the same page. How can I make it intent to another activity when it is change to editText ? Thanks .

Activity A

  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.create_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;
        }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_search:
                Intent intent=new Intent(A.this, B.class);
                startActivity(intent);
                return true;

Activity B

  public class SearchResultActivity extends ActionBarActivity {


        TextView text;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
            text=(TextView)findViewById(R.id.textView1);
            handleIntent(getIntent());
        }


 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.create_menu, menu);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return true;
    }




        @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);
                text.setText(query);
                //use the query to search
            }
        }
    }

This is what I want to achieve.

Activity A

When search icon is clicked, change to editText in Activity B and display the related result below the app bar.

Activity B

解决方案

Please follow below steps:

first.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/m1"
    android:title="Search"
    android:showAsAction="always"
    android:icon="@android:drawable/ic_menu_search"/>

MainActivityFirst.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.first,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.m1){
        Intent i = new Intent(this,MainActivitySecond.class);
        startActivity(i);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

second.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"/>

MainActivitySecond.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.second, menu);
    SearchView searchView =
    (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setIconified(false);
        return super.onCreateOptionsMenu(menu);
    }

searchView.setIconified(false) will show the search view expanded in second activity, here you can play with search view, suggestions etc. For more follow Creating search interface in android

Hope it will solve your issue.

这篇关于意向下一个活动时,点击搜索小工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆