打开 ListAdapter 项目的软键盘,其中包含带有字符串的视图

发布于 2024-09-11 14:51:49 字数 4361 浏览 5 评论 0原文

我有一个 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>

包含 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>

long getView 函数的 XML:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贱贱哒 2024-09-18 14:51:49

您是否尝试在 EditText 上使用 dispatchTouchEvent(android.view.MotionEvent)

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

往事随风而去 2024-09-18 14:51:49

尝试在 TextEdit 控件上的 xml 中设置 android:focusable="true" 或在 Java 代码中设置 setFocusable(true)

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

蓝眼睛不忧郁 2024-09-18 14:51:49

尝试将 android:descendantFocusability="blocksDescendants" 添加到 中。有时需要这样做,以便列表中的小部件获得正确的焦点。

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

断爱 2024-09-18 14:51:49

您可以尝试将 添加到 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,选择 请求焦点。希望它能起作用...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文