如何重置操作栏中的图标

时间:2013-03-24 23:14:28

标签: android android-actionbar

我在Android程序中向ActionBar添加了一个EditText元素。单击该图标时,将显示EditText元素,并在触发return或enter键时输入和捕获数据。如何使EditText元素消失并重新显示图标?目前,EditText字段仍保留在ActionBar中,并隐藏图标。下面是我的菜单xml文件。任何帮助表示赞赏。

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
  <item
    android:id="@+id/herbsearch"
    android:title="HerbSearch"
    android:showAsAction="always"
    android:icon="@android:drawable/ic_menu_search"
    />
   <item
    android:id="@+id/locationchange"
    android:title="Change Location"
    android:showAsAction="ifRoom|collapseActionView"
    android:icon="@drawable/ic_launcher"
    android:actionLayout="@layout/search_layout"
    />

这是我的EditText布局。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<EditText
    android:id="@+id/txt_search"
    android:inputType="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:textColor="#000000"
    android:textSize="15sp"
     android:singleLine="true"
     android:hint="Enter new location"
    android:imeOptions="actionDone"
/>

以下是我的onCreateOptionsMenu逻辑。

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    /** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id.locationchange).getActionView();

    /** Get the edit text from the action view */
    EditText locSearch = ( EditText ) v.findViewById(R.id.txt_search);
    locSearch.requestFocus();
    /** Setting an action listener */
    locSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        //    Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();

            GetMap(String.valueOf(v.getText()));
            invalidateOptionsMenu();

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

        // I tried these next 3 instructions with no success
        //   locSearch.setVisibility(1);   

         //  v.setTextColor(Color.WHITE);

        //   v.setVisibility(1);
            return false;

        }
    });
   }

1 个答案:

答案 0 :(得分:1)

试试这个:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    final MenuItem locationItem = menu.findItem(R.id.locationchange);
    View v = (View) locationItem.getActionView();
    EditText locSearch = (EditText) v.findViewById(R.id.txt_search);
    locSearch.requestFocus();

    locSearch.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                locationItem.collapseActionView();
                return true;
            }
            return false;
        }
    });
}

我添加了最后一行MenuItem locationItem = menu.findItem(R.id.locationchange);检索位置更改菜单项,我正在调用locationItem.collapseActionView();摆脱动作视图/搜索视图。