如何在android活动中为搜索小部件添加折叠视图?

时间:2015-01-13 09:57:13

标签: android android-edittext searchview

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {

            inflater.inflate(R.menu.main_activity_bar_l, menu);
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {

            inflater.inflate(R.menu.main_activity_bar, menu);
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {

            inflater.inflate(R.menu.main_activity_bar, menu);
        } else {

            inflater.inflate(R.menu.main_activity_bar, menu);
        }
        View v = (View) menu.findItem(R.id.my_action).getActionView();


        /** Get the edit text from the action view */
        txtSearch = (EditText) v.findViewById(R.id.myActionEditText);

//      edit_searchtext = txtSearch.getText().toString();
        /** Setting an action listener */

        ImageView Search = (ImageView) v.findViewById(R.id.search);
        Search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                search_value = txtSearch.getText().toString();
                if (txtSearch.getText().toString().equals("")) {
                    Toast.makeText(getBaseContext(),
                            "Please enter search values", Toast.LENGTH_SHORT)
                            .show();
                } else {

                    Intent intent = new Intent(MainActivity.this,
                            Display.class);
                    intent.putExtra("Action_search", txtSearch.getText()
                            .toString());
                    startActivity(intent);
                    txtSearch.setText("");
                }
            }

        });

        return super.onCreateOptionsMenu(menu);
    }

我在菜单项中创建了一个带有折叠视图操作的cutomized searchview,但搜索编辑文本在输入搜索值并单击搜索按钮时不会崩溃。

我的菜单栏活动是:

<item android:id="@+id/my_action"
          android:icon="@drawable/ic_action_search"
          android:title="My Action"
          android:showAsAction="always|collapseActionView"
          android:actionLayout="@layout/my_action" />

一旦我输入搜索值并点击imageview搜索,我怎么能折叠edittext 请帮忙

0 个答案:

没有答案