android 键盘在覆盖EditText中未聚焦,键盘按预期打开,但无法正常工作

nwo49xxi  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(185)

当我在键盘上输入任何东西,然后谷歌打开。无法在此EditText视图中键入内容。在这里,我创建了覆盖服务。这里我写了强制打开软键盘逻辑的代码。www.example.com 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("mFloatingWidgetView" +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. }

这里我有修改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"// this is for overlayKeyBoard
  12. >

这是覆盖模式的布局,我在这里添加了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. <!-- Root container of Floating Widget View -->
  7. <RelativeLayout
  8. android:id="@+id/root_container"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. <!-- View while view is collapsed -->
  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 of floating widget -->
  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. <!-- Close button to close Floating Widget View -->
  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. <!-- View while view is expanded -->
  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="Enter your comment"
  63. android:textColor="@android:color/black"
  64. android:textSize="14sp" />
  65. </LinearLayout>
  66. <!-- ImageView to Close Expanded View -->
  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 to Open Activity -->
  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中键入文本。

vc6uscn9

vc6uscn91#

  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);

你不需要这些。设定焦点就足够了。特别是这里的第2行和第4行是错误的--它们都将显示键盘,但没有将其正确地连接到InputConnection,从而导致键盘出现但什么也不做。

相关问题