【问题标题】:Soft keyboard covers an EditText in a PopupWindow软键盘覆盖 PopupWindow 中的 EditText
【发布时间】:2012-06-03 06:48:25
【问题描述】:

我整理了一个简单的测试项目,它显示了一个包含 EditText 的 PopupWindow(在 Android 2.2 上)。当我点击 EditText 时,会显示软键盘,正如我所期望的那样。但是,软键盘覆盖了 EditText,我无法以我认为应该的方式平移屏幕以保持 EditText 处于视图中。我的代码:

TestAdjustPanActivity.java:

package com.example;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.PopupWindow;

public class TestAdjustPanActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final PopupWindow pw = new PopupWindow(this);
        pw.setContentView(getLayoutInflater().inflate(R.layout.popup, null, false));
        pw.setWidth(400);
        pw.setHeight(600);
        pw.setFocusable(true);

        pw.setOutsideTouchable(true);
        pw.setTouchable(true);
        pw.setBackgroundDrawable(new BitmapDrawable());

        // This is the line referred to in the edit:
        pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        findViewById(R.id.main).post(new Runnable() {
            public void run() {
            pw.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);
            }
        });
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

popup.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#666666"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/current_password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="300dp" />
    </LinearLayout>

</ScrollView>

...而且我的 AndroidManifest.xml 确实在标签中包含 android:windowSoftInputMode="stateUnspecified|adjustPan" 行,用于应用程序中唯一的活动。

有什么想法吗? SO上与此问题相关的其他帖子都没有为我完成。我错过了什么让视图无法按照我期望的方式平移?

编辑:我尝试按照指示在 TestAdjustPanActivity 中添加一行,这导致屏幕在我拥有的 Android 3.2 设备上完美平移。但是,它仍然无法在我的 Android 2.2 设备上平移,这让这更加令人困惑。

【问题讨论】:

    标签: java android xml android-edittext android-softkeyboard


    【解决方案1】:

    对于将来遇到此问题的任何人,我最终只使用了 Dialog 而不是 PopupWindow,并且在 2.2 中使用 Dialog 平移以保持 EditText 处于视图中可以正常工作。

    【讨论】:

      【解决方案2】:

      有更简单的方法。只需为活动键盘提供替代布局即可。

      1. 左键单击项目选择:“Android 工具/新资源文件...”。
      2. 选择布局,给文件名“main”(不用担心冲突)。
      3. 单击“下一步”。然后在左侧列表中选择“键盘”并将其向右移动(单击“->”)。
      4. 在右侧选择键盘状态。
      5. 单击完成。
      6. 现在将位于“res/layout”中的 main.xml 的内容复制到 res/layout-keyssoft 中的新文件中。
      7. 更正新的布局,使键盘不是这样。请记住为这两个布局中的各个组件保持相同的“id”(这就是需要复制粘贴的原因)。
      8. 享受它的工作原理

      阅读 configuration changes 以了解其工作原理。请注意,每次配置更改(方向更改、语言更改……)都会导致重新创建 Activity(在这种情况下,onCreate 的参数不为空),因此您可以为不同的情况提供不同的布局。

      【讨论】:

      • 根据 Android 文档,这仅适用于键盘的实际可用性发生变化时,不幸的是,它不适用于简单地显示或隐藏时。然而,他们确实提到了 Configuration 类的 keyboardHidden 属性,我来看看。谢谢!
      【解决方案3】:

      我怀疑问题是因为在XML 中使用了ScrollView

      还有一个类似的问题,你可以check all different answers there,可能对你有用。

      【讨论】:

        【解决方案4】:

        改一下

        android:windowSoftInputMode="stateUnspecified|adjustPan"
        

        android:windowSoftInputMode="adjustPan"
        

        仅在您的清单文件中

        【讨论】:

          【解决方案5】:

          你应该改变

          android:windowSoftInputMode="stateUnspecified|adjustPan 
          

          android:windowSoftInputMode="stateHidden|adjustPan"
          

          【讨论】:

          • 嗯,不幸的是没有运气。不过谢谢!
          【解决方案6】:

          花了几个小时才弄明白,最终我使用了 scrollview

          当您使用弹出窗口和软输入键盘隐藏您的视图时,显然没有办法克服这个问题。只需选择滚动视图即可。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多