Открытая программная клавиатура для элемента ListAdapter с представлением со строкой - PullRequest
6 голосов
/ 28 июля 2010

У меня ListAdapter с в этом случае содержит один элемент. Этот элемент создает представление, которое имеет EditText.

Когда отображается вид, я хочу открыть программную клавиатуру, чтобы позволить пользователю редактировать поле.

Я попытался вызвать функцию getView () адаптера:

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

который, похоже, ничего не делает.

Я тоже пробовал:

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

По какой-то таинственной причине создается цикл, в котором программная клавиша открывается и закрывается повторно.

Когда я удаляю requestFocus и нажимаю на кнопку EditText в программе, она открывает экранную клавиатуру, но выясняет, есть ли там requestFocus showSoftInput, похоже, не работает.

Что мне делать?

XML элемента списка:

<?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, содержащий 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_content"
        android:layout_below="@+id/Button01"
        android:gravity="center_horizontal"
        />
</RelativeLayout>

длинная функция getView:

@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;
    }
}

Ответы [ 4 ]

1 голос
/ 30 июля 2010

Вы пытались использовать dispatchTouchEvent(android.view.MotionEvent) на EditText?

0 голосов
/ 09 января 2012

Можете ли вы попробовать добавить <requestFocus /> в ваш 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>

Или вы можете щелкнуть правой кнопкой мыши на EditText в режиме конструктора, выбрать Request Focus. Надеюсь, это сработает ...

0 голосов
/ 20 сентября 2010

Попробуйте добавить android:descendantFocusability="blocksDescendants" к вашему <RelativeLayout />. Иногда это необходимо, чтобы виджеты в списках получали правильную фокусировку.

0 голосов
/ 28 июля 2010

попробуйте установить android:focusable="true" в xml или setFocusable(true) в java-коде на вашем элементе управления TextEdit

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...