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

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

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

问题

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

  1. 当我在键盘上输入任何内容时谷歌会打开无法在此EditText视图中输入
  2. 在这里我创建了悬浮服务并编写了打开软键盘的代码逻辑
  3. floatingPointWidthService.java
  4. ```java
  5. public void onCreate() {
  6. super.onCreate();
  7. // 初始化WindowManager
  8. mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  9. getWindowManagerDefaultDisplay();
  10. // 初始化LayoutInflater
  11. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  12. addRemoveView(inflater);
  13. addFloatingWidgetView(inflater);
  14. implementClickListeners();
  15. implementTouchListenerToFloatingWidgetView();
  16. System.out.println("mFloatingWidgetView" + mFloatingWidgetView);
  17. expandedView = mFloatingWidgetView.findViewById(R.id.expanded_container);
  18. expandedView.setVisibility(View.VISIBLE); // 设置大视图打开
  19. edittext1 = mFloatingWidgetView.findViewById(R.id.floating_widget_title_label);
  20. edittext1.requestFocus();
  21. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  22. imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
  23. imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
  24. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  25. }

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

  1. <application
  2. android:allowBackup="true"
  3. android:dataExtractionRules="@xml/data_extraction_rules"
  4. android:fullBackupContent="@xml/backup_rules"
  5. android:icon="@mipmap/ic_launcher"
  6. android:label="@string/app_name"
  7. android:roundIcon="@mipmap/ic_launcher_round"
  8. android:supportsRtl="true"
  9. android:theme="@style/Theme.ModFill"
  10. tools:targetApi="31"
  11. android:windowSoftInputMode="adjustPan">// 这是为overlayKeyBoard的设置
  12. </application>

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <!-- 悬浮小部件视图的根容器 -->
  7. <RelativeLayout
  8. android:id="@+id/root_container"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. <!-- 视图折叠时显示的内容 -->
  12. <RelativeLayout
  13. android:id="@+id/collapse_view"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:orientation="vertical"
  17. android:visibility="visible">
  18. <!-- 悬浮小部件的ImageView -->
  19. <ImageView
  20. android:id="@+id/collapsed_iv"
  21. android:layout_width="70dp"
  22. android:layout_height="70dp"
  23. android:layout_marginTop="8dp"
  24. android:src="@mipmap/ic_launcher_round"
  25. tools:ignore="ContentDescription" />
  26. <!-- 用于关闭悬浮小部件视图的关闭按钮 -->
  27. <ImageView
  28. android:id="@+id/close_floating_view"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginLeft="50dp"
  32. android:layout_marginTop="5dp"
  33. android:background="@drawable/circle_shape"
  34. android:src="@drawable/ic_close_white_24dp"
  35. tools:ignore="ContentDescription" />
  36. </RelativeLayout>
  37. <!-- 视图展开时显示的内容 -->
  38. <LinearLayout
  39. android:id="@+id/expanded_container"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:background="@android:color/white"
  43. android:gravity="center"
  44. android:orientation="horizontal"
  45. android:padding="8dp"
  46. android:visibility="gone">
  47. <ImageView
  48. android:id="@+id/floating_widget_image_view"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:src="@mipmap/ic_launcher" />
  52. <LinearLayout
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:layout_marginLeft="5dp"
  56. android:orientation="vertical">
  57. <EditText
  58. android:id="@+id/floating_widget_title_label"
  59. android:layout_width="120dp"
  60. android:layout_height="wrap_content"
  61. android:gravity="center_vertical"
  62. android:hint="输入您的评论"
  63. android:textColor="@android:color/black"
  64. android:textSize="14sp" />
  65. </LinearLayout>
  66. <!-- 用于关闭展开视图的ImageView -->
  67. <ImageView
  68. android:id="@+id/close_expanded_view"
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:layout_weight="1"
  72. android:padding="10dp"
  73. android:src="@drawable/ic_close_black_24dp" />
  74. <!-- 用于打开活动的ImageView -->
  75. <ImageView
  76. android:id="@+id/open_activity_button"
  77. android:layout_width="match_parent"
  78. android:layout_height="wrap_content"
  79. android:layout_weight="1"
  80. android:padding="10dp"
  81. android:src="@drawable/ic_aspect_ratio_black_24dp" />
  82. </LinearLayout>
  83. </RelativeLayout>
  84. </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

  1. public void onCreate() {
  2. super.onCreate();
  3. //init WindowManager
  4. mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  5. getWindowManagerDefaultDisplay();
  6. //Init LayoutInflater
  7. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  8. addRemoveView(inflater);
  9. addFloatingWidgetView(inflater);
  10. implementClickListeners();
  11. implementTouchListenerToFloatingWidgetView();
  12. System.out.println(&quot;mFloatingWidgetView&quot; +mFloatingWidgetView);
  13. expandedView = mFloatingWidgetView.findViewById(R.id.expanded_container);
  14. expandedView.setVisibility(View.VISIBLE);//set large view open
  15. edittext1 = mFloatingWidgetView.findViewById(R.id.floating_widget_title_label);
  16. edittext1.requestFocus();
  17. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  18. imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
  19. imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
  20. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  21. }

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

  1. &lt;application
  2. android:allowBackup=&quot;true&quot;
  3. android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
  4. android:fullBackupContent=&quot;@xml/backup_rules&quot;
  5. android:icon=&quot;@mipmap/ic_launcher&quot;
  6. android:label=&quot;@string/app_name&quot;
  7. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  8. android:supportsRtl=&quot;true&quot;
  9. android:theme=&quot;@style/Theme.ModFill&quot;
  10. tools:targetApi=&quot;31&quot;
  11. android:windowSoftInputMode=&quot;adjustPan&quot;// this is for overlayKeyBoard
  12. &gt;

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;wrap_content&quot;
  5. android:layout_height=&quot;wrap_content&quot;&gt;
  6. &lt;!-- Root container of Floating Widget View --&gt;
  7. &lt;RelativeLayout
  8. android:id=&quot;@+id/root_container&quot;
  9. android:layout_width=&quot;wrap_content&quot;
  10. android:layout_height=&quot;wrap_content&quot;&gt;
  11. &lt;!-- View while view is collapsed --&gt;
  12. &lt;RelativeLayout
  13. android:id=&quot;@+id/collapse_view&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:orientation=&quot;vertical&quot;
  17. android:visibility=&quot;visible&quot;&gt;
  18. &lt;!-- ImageView of floating widget --&gt;
  19. &lt;ImageView
  20. android:id=&quot;@+id/collapsed_iv&quot;
  21. android:layout_width=&quot;70dp&quot;
  22. android:layout_height=&quot;70dp&quot;
  23. android:layout_marginTop=&quot;8dp&quot;
  24. android:src=&quot;@mipmap/ic_launcher_round&quot;
  25. tools:ignore=&quot;ContentDescription&quot; /&gt;
  26. &lt;!-- Close button to close Floating Widget View --&gt;
  27. &lt;ImageView
  28. android:id=&quot;@+id/close_floating_view&quot;
  29. android:layout_width=&quot;wrap_content&quot;
  30. android:layout_height=&quot;wrap_content&quot;
  31. android:layout_marginLeft=&quot;50dp&quot;
  32. android:layout_marginTop=&quot;5dp&quot;
  33. android:background=&quot;@drawable/circle_shape&quot;
  34. android:src=&quot;@drawable/ic_close_white_24dp&quot;
  35. tools:ignore=&quot;ContentDescription&quot; /&gt;
  36. &lt;/RelativeLayout&gt;
  37. &lt;!-- View while view is expanded --&gt;
  38. &lt;LinearLayout
  39. android:id=&quot;@+id/expanded_container&quot;
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:background=&quot;@android:color/white&quot;
  43. android:gravity=&quot;center&quot;
  44. android:orientation=&quot;horizontal&quot;
  45. android:padding=&quot;8dp&quot;
  46. android:visibility=&quot;gone&quot;&gt;
  47. &lt;ImageView
  48. android:id=&quot;@+id/floating_widget_image_view&quot;
  49. android:layout_width=&quot;wrap_content&quot;
  50. android:layout_height=&quot;wrap_content&quot;
  51. android:src=&quot;@mipmap/ic_launcher&quot; /&gt;
  52. &lt;LinearLayout
  53. android:layout_width=&quot;wrap_content&quot;
  54. android:layout_height=&quot;wrap_content&quot;
  55. android:layout_marginLeft=&quot;5dp&quot;
  56. android:orientation=&quot;vertical&quot;&gt;
  57. &lt;EditText
  58. android:id=&quot;@+id/floating_widget_title_label&quot;
  59. android:layout_width=&quot;120dp&quot;
  60. android:layout_height=&quot;wrap_content&quot;
  61. android:gravity=&quot;center_vertical&quot;
  62. android:hint=&quot;Enter your comment&quot;
  63. android:textColor=&quot;@android:color/black&quot;
  64. android:textSize=&quot;14sp&quot; /&gt;
  65. &lt;/LinearLayout&gt;
  66. &lt;!-- ImageView to Close Expanded View --&gt;
  67. &lt;ImageView
  68. android:id=&quot;@+id/close_expanded_view&quot;
  69. android:layout_width=&quot;match_parent&quot;
  70. android:layout_height=&quot;wrap_content&quot;
  71. android:layout_weight=&quot;1&quot;
  72. android:padding=&quot;10dp&quot;
  73. android:src=&quot;@drawable/ic_close_black_24dp&quot; /&gt;
  74. &lt;!-- ImageView to Open Activity --&gt;
  75. &lt;ImageView
  76. android:id=&quot;@+id/open_activity_button&quot;
  77. android:layout_width=&quot;match_parent&quot;
  78. android:layout_height=&quot;wrap_content&quot;
  79. android:layout_weight=&quot;1&quot;
  80. android:padding=&quot;10dp&quot;
  81. android:src=&quot;@drawable/ic_aspect_ratio_black_24dp&quot; /&gt;
  82. &lt;/LinearLayout&gt;
  83. &lt;/RelativeLayout&gt;
  84. &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,因此会导致键盘出现但无法正常工作。

英文:
  1. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  2. imm.showSoftInputFromInputMethod(edittext1.getApplicationWindowToken(), 1);
  3. imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
  4. 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:

确定