简体   繁体   中英

Click on EditText doesn't show softkeyboard

I am using ActionBarSherlock, in the ActionBar i have this custom layout to appear when i click on the 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>

So, 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.

Its fine till here.

The problem is:

i close the softkeyboard using device back button ---> then close the custom layout using ActionBar Home icon click ---> now 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 (here is the problem).

Please help, i have no idea why its not working as expected.

Thank You

I have faced this problem before and here is my solution

//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 ~ your searchET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM