0

我正在使用 ActionBarSherlock,在 ActionBar 中,当我单击 searchIcon 时,我会显示此自定义布局。

public boolean onCreateOptionsMenu(final Menu menu) {
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.on_search_icon_clicked,null, false);
view.findViewById(R.id.search_image).setOnClickListener(
        new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

        EditText searchET = (EditText) view.findViewById(R.id.search_editText);
        String s = searchET.getText().toString();
        Toast.makeText(getBaseContext(), "Searching for: " + s,1000).show();
                    }
        });

menu.add("Search")
        .setIcon(R.drawable.ic_search)
        .setActionView(view)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        return true;
}

on_search_icon_clicked.xml

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

    <ImageView
        android:id="@+id/search_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_search"
        android:clickable="true"
        android:contentDescription="@string/seach_icon" />

    <EditText
        android:id="@+id/search_editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/search_image"
        android:hint="@string/search"
        android:singleLine="true" />

</RelativeLayout>

所以,i click on the SearchIcon---> i get the custom layout with editText and ImageView---> click on editText, opens softkeyboard---> enters string and clicks the imageView--->shows me the toast.

到这里为止都很好。

问题是:

close the softkeyboard using device back button---> 然后close the custom layout using ActionBar Home icon click---> 现在click the searchIcon again, which reopens the custom layout, with editText filled with previously searched string---> click on the editText, the softkeyboard doesn't open up(这是问题所在)。

请帮忙,我不知道为什么它不能按预期工作。

谢谢你

4

1 回答 1

1

我以前遇到过这个问题,这是我的解决方案

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

<item
    android:id="@+id/menu_search"
    android:orderInCategory="0"
    android:icon="@drawable/btn_search"
    android:title="Search"
    android:actionLayout="@layout/on_search_icon_clicked"
    android:showAsAction="always|collapseActionView"
   />
</menu>


@Override
public boolean onCreateOptionsMenu(Menu menu)
{       
    getSupportMenuInflater().inflate(R.menu.your_menu, menu);
    mSearchbar = (MenuItem) menu.findItem(R.id.menu_search);
    View actionview = mSearchbar.getActionView();
    mEtSearchbar = ((EditText) actionview.findViewById(R.id.search_editText));
    mEtSearchbar.setOnEditorActionListener(this);       
    mEtSearchbar.addTextChangedListener(this);      
    super.onCreateOptionsMenu(menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    Intent intent;
    switch (item.getItemId())
    {
        case R.id.menu_search:                  
            mEtSearchbar.clearFocus();
             (new Handler()).postDelayed(new Runnable() {
                public void run() {
                    mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                    mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
                }
             }, 100);
            break;          
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

mEtSearchbar ~ 你的搜索ET

于 2012-08-27T08:33:50.957 回答