英文:
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中输入文本。
英文:
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("mFloatingWidgetView" +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="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"// this is for overlayKeyBoard
>
This is layout of overlay modal here I have added the 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">
<!-- Root container of Floating Widget View -->
<RelativeLayout
android:id="@+id/root_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- View while view is collapsed -->
<RelativeLayout
android:id="@+id/collapse_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<!-- ImageView of floating widget -->
<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" />
<!-- Close button to close Floating Widget View -->
<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>
<!-- View while view is expanded -->
<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="Enter your comment"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
<!-- ImageView to Close Expanded View -->
<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 to Open Activity -->
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论