0

我在一个 android 程序中向我的 ActionBar 添加了一个 EditText 元素。单击该图标时,将出现 EditText 元素,并在按下回车键或回车键时输入和捕获数据。如何使 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;

        }
    });
   }
4

1 回答 1

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(); 摆脱动作视图/搜索视图。

于 2013-03-25T01:04:27.963 回答