键盘未聚焦在覆盖的EditText上。键盘如预期般弹出,但无法正常使用。

huangapple go评论84阅读模式
英文:

Keyboard not focusing in overlay EditText. Keyboard is opening as expected but it is not working

问题

这是您提供的代码的翻译:

当我在键盘上输入任何内容时谷歌会打开无法在此EditText视图中输入
在这里我创建了悬浮服务并编写了打开软键盘的代码逻辑
floatingPointWidthService.java
```java
public void onCreate() {
    super.onCreate();
    // 初始化WindowManager
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    getWindowManagerDefaultDisplay();
    // 初始化LayoutInflater
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    addRemoveView(inflater);
    addFloatingWidgetView(inflater);
    implementClickListeners();
    implementTouchListenerToFloatingWidgetView();
    System.out.println("mFloatingWidgetView" + mFloatingWidgetView);
    expandedView = mFloatingWidgetView.findViewById(R.id.expanded_container);
    expandedView.setVisibility(View.VISIBLE); // 设置大视图打开
    edittext1 = mFloatingWidgetView.findViewById(R.id.floating_widget_title_label);
    edittext1.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
    imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

在这里,我更改了android:windowSoftInputMode="adjustPan"
AndroidManifest.xml

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.ModFill"
    tools:targetApi="31"
    android:windowSoftInputMode="adjustPan">// 这是为overlayKeyBoard的设置
</application>

这是悬浮模态的布局,我在其中添加了EditText。
floating_width_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- 悬浮小部件视图的根容器 -->
    <RelativeLayout
        android:id="@+id/root_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- 视图折叠时显示的内容 -->

        <RelativeLayout
            android:id="@+id/collapse_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <!-- 悬浮小部件的ImageView -->
            <ImageView
                android:id="@+id/collapsed_iv"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_marginTop="8dp"
                android:src="@mipmap/ic_launcher_round"
                tools:ignore="ContentDescription" />

            <!-- 用于关闭悬浮小部件视图的关闭按钮 -->
            <ImageView
                android:id="@+id/close_floating_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/circle_shape"
                android:src="@drawable/ic_close_white_24dp"
                tools:ignore="ContentDescription" />
        </RelativeLayout>

        <!-- 视图展开时显示的内容 -->
        <LinearLayout
            android:id="@+id/expanded_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="8dp"
            android:visibility="gone">

            <ImageView
                android:id="@+id/floating_widget_image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/floating_widget_title_label"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:hint="输入您的评论"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />

            </LinearLayout>

            <!-- 用于关闭展开视图的ImageView -->
            <ImageView
                android:id="@+id/close_expanded_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dp"
                android:src="@drawable/ic_close_black_24dp" />

            <!-- 用于打开活动的ImageView -->
            <ImageView
                android:id="@+id/open_activity_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dp"
                android:src="@drawable/ic_aspect_ratio_black_24dp" />
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

键盘通过最后一行imm.toggleSoftInput方法打开。请告诉如何在此EditText中输入文本。

英文:

键盘未聚焦在覆盖的EditText上。键盘如预期般弹出,但无法正常使用。

when I type anything on keyboard then google opening. Not able to type in this EditText view.
Here I have created overlay services. and here I have written the codes for force open soft Keyboard logic.
floatingPointWidthService.java

 public void onCreate() {
super.onCreate();
//init WindowManager
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
getWindowManagerDefaultDisplay();
//Init LayoutInflater
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
addRemoveView(inflater);
addFloatingWidgetView(inflater);
implementClickListeners();
implementTouchListenerToFloatingWidgetView();
System.out.println(&quot;mFloatingWidgetView&quot; +mFloatingWidgetView);
expandedView = mFloatingWidgetView.findViewById(R.id.expanded_container);
expandedView.setVisibility(View.VISIBLE);//set large view open
edittext1 = mFloatingWidgetView.findViewById(R.id.floating_widget_title_label);
edittext1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

Here I have changes android:windowSoftInputMode=&quot;adjustPan&quot;
AndroidManifest.xml

&lt;application
android:allowBackup=&quot;true&quot;
android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
android:fullBackupContent=&quot;@xml/backup_rules&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/Theme.ModFill&quot;
tools:targetApi=&quot;31&quot;
android:windowSoftInputMode=&quot;adjustPan&quot;// this is for overlayKeyBoard
&gt;

This is layout of overlay modal here I have added the EditText.
floating_width_layout.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;!--   Root container of Floating Widget View  --&gt;
&lt;RelativeLayout
android:id=&quot;@+id/root_container&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;!--  View while view is collapsed  --&gt;
&lt;RelativeLayout
android:id=&quot;@+id/collapse_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:visibility=&quot;visible&quot;&gt;
&lt;!--   ImageView of floating widget  --&gt;
&lt;ImageView
android:id=&quot;@+id/collapsed_iv&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:src=&quot;@mipmap/ic_launcher_round&quot;
tools:ignore=&quot;ContentDescription&quot; /&gt;
&lt;!--   Close button to close Floating Widget View  --&gt;
&lt;ImageView
android:id=&quot;@+id/close_floating_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;50dp&quot;
android:layout_marginTop=&quot;5dp&quot;
android:background=&quot;@drawable/circle_shape&quot;
android:src=&quot;@drawable/ic_close_white_24dp&quot;
tools:ignore=&quot;ContentDescription&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;!--   View while view is expanded   --&gt;
&lt;LinearLayout
android:id=&quot;@+id/expanded_container&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@android:color/white&quot;
android:gravity=&quot;center&quot;
android:orientation=&quot;horizontal&quot;
android:padding=&quot;8dp&quot;
android:visibility=&quot;gone&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/floating_widget_image_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:src=&quot;@mipmap/ic_launcher&quot; /&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;5dp&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;EditText
android:id=&quot;@+id/floating_widget_title_label&quot;
android:layout_width=&quot;120dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_vertical&quot;
android:hint=&quot;Enter your comment&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;14sp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;!--  ImageView to Close Expanded View  --&gt;
&lt;ImageView
android:id=&quot;@+id/close_expanded_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_weight=&quot;1&quot;
android:padding=&quot;10dp&quot;
android:src=&quot;@drawable/ic_close_black_24dp&quot; /&gt;
&lt;!--  ImageView to Open Activity  --&gt;
&lt;ImageView
android:id=&quot;@+id/open_activity_button&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_weight=&quot;1&quot;
android:padding=&quot;10dp&quot;
android:src=&quot;@drawable/ic_aspect_ratio_black_24dp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/RelativeLayout&gt;
&lt;/FrameLayout&gt;

keyboard is opening with last line imm.toggleSoftInput method. Please tell how to type text in this EditText.

答案1

得分: 1

不需要这些。设置焦点就足够了。特别是这里的第2行和第4行是错误的 - 它们都会显示键盘,但不会正确地连接到InputConnection,因此会导致键盘出现但无法正常工作。

英文:
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

You don't need any of that. Setting the focus is sufficient. And in particular the 2nd and 4th lines here are wrong- both of them will show the keyboard, but not hook it up properly to the InputConnection and thus lead to a keyboard that appears but does nothing.

huangapple
  • 本文由 发表于 2023年6月15日 20:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定