开发者

Open soft keyboard for ListAdapter item with contains a view with a string

I have a ListAdapter with in this case contains a single item. That item produces a view that has an EditText.

When the view get displayed I want to open the soft keyboard to allow the user to edit the field.

I tried to call in the getView() function of the adapter:

View edittext = view.findViewById(R.id.EditText01);
edittext.requestFocus();
mInputMgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);

which doesn't seem to do anything.

I also tried:

View edittext = view.findViewById(R.id.EditText01);
edittext.requestFocus();
mInputMgr.toggleSoftInput(0, 0);

That produces for some mysterious reason a loop in which the softkeyboard gets opened and closed repeatably.

When I remove the requestFocus and touch click on the EditText in the program it pops up the softkeyboard but regardles of whether requestFocus is there showSoftInput doesn't seem to work.

What do I have to do?

XML of the list element:

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

  android:focusable="true"
  android:background="@color/light_blue"
  >

<EditText android:id="@+id/EditText01" 
android:layout_height="wrap_content" 
android:text="" 
android:focusable="true"
android:layout_width="fill_parent" android:inputType="textShortMessage"></EditText>
</RelativeLayout>

XML that contains the ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  >
    <Button android:text="@+id/Button01" 
        android:id="@+id/Button01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <ListView android:id="@+id/ListView01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_开发者_StackOverflow社区content"
        android:layout_below="@+id/Button01"
        android:gravity="center_horizontal"
        />
</RelativeLayout>

long getView function:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView != null){
        return convertView;
    }

    if (mDialog!=null){
        final Choice thisChoice = mDialog.getChoice(
                position, 
                mContentRes);
        Log.v(TAG, "DialogAdapter: "+ thisChoice.toString());

        View view = thisChoice.getView(
                mInflater, parent, mFillInput, mDialog);
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                if (thisChoice.choiceSingleClick(mDialog)){
                    Log.i(TAG, 
                            "Single Click selected Choice: " 
                            + thisChoice.toString());
                    Log.d(TAG, 
                            "getNextInputId: " 
                            + Integer.toString(
                                    thisChoice.getNextInputId()));
                    mFillInput.renderNext(thisChoice.getNextInputId());
                }
                else{
                    mFillInput.checkSelectedStatus();
                }
            }
        };
        view.setOnClickListener(listener);

        View.OnLongClickListener longListener = new View.OnLongClickListener() {
            public boolean onLongClick(View v) {            
                thisChoice.choiceLongClick(mDialog);
                return false;
            }
        };
        view.setOnLongClickListener(longListener);

        Log.d(TAG, "thisChoice: " + thisChoice);
        Log.d(TAG, "selected: " + thisChoice.isSelected());
        if((position == 0 && mDialog.getSelectedCount() == 0) ||
                thisChoice.isSelected()){
            view.requestFocus();
        }


        if (thisChoice.isEnterStringChoice() & position==0){
            Log.d(TAG, "Request Focus for StringChoice");
            View edittext = view.findViewById(R.id.EditText01);
            edittext.requestFocus();
            //edittext.dispatchTouchEvent(null);
            //FillInput.mInputMgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
            //mFillInput.mInputMgr.toggleSoftInput(0, 0);
        }

        return view;
    }           
    else{
        return null;
    }
}


Did you try to use dispatchTouchEvent(android.view.MotionEvent) on the EditText ?


try to set android:focusable="true" in xml or setFocusable(true) in java code on your TextEdit control


Try adding android:descendantFocusability="blocksDescendants" to your <RelativeLayout />. That is sometimes needed to make it so that widgets in lists get the right focus.


Can you try adding <requestFocus /> into your EditText:

<EditText android:id="@+id/EditText01" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:focusable="true"
    android:layout_width="fill_parent" android:inputType="textShortMessage">

    <requestFocus />
</EditText>

Or you can right click on the EditText in design mode, select Request Focus. Hope it will work...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜