Java 软键盘覆盖弹出窗口中的编辑文本

Java 软键盘覆盖弹出窗口中的编辑文本,java,android,xml,android-edittext,android-softkeyboard,Java,Android,Xml,Android Edittext,Android Softkeyboard,我创建了一个简单的测试项目,它显示一个包含EditText的弹出窗口(在Android 2.2上)。当我点击编辑文本时,如我所料,软键盘会显示出来。然而,软键盘覆盖了EditText,我无法让屏幕以我认为应该的方式平移以保持EditText在视图中。我的代码: TestAdjustPanActivity.java: package com.example; import android.app.Activity; import android.graphics.drawable.BitmapD

我创建了一个简单的测试项目,它显示一个包含EditText的弹出窗口(在Android 2.2上)。当我点击编辑文本时,如我所料,软键盘会显示出来。然而,软键盘覆盖了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>

…而my AndroidManifest.xml在应用程序中唯一活动的标记中确实包含一行
android:windowSoftInputMode=“stateUnspecified | adjustPan”

有什么想法吗?其他与这个问题相关的帖子都没有为我做过。我错过了什么,阻止了视图以我期望的方式平移

编辑:如图所示,我尝试在TestAdjustPanActivity中添加一行,这使得屏幕在我拥有的Android 3.2设备上可以完美地平移。然而,它仍然不能在我的Android 2.2设备上运行,这使得它更加混乱。

你应该改变

android:windowSoftInputMode="stateUnspecified|adjustPan 


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

  • 左键单击项目选择:“Android工具/新资源文件…”
  • 选择布局,给文件名“main”(不要担心冲突)
  • 单击“下一步”。然后在左侧的列表中选择“键盘”并将其向右移动(单击“->”)
  • 在右侧选择键盘状态
  • 单击finish
  • 现在将“res/layout”中main.xml的内容复制到“res/layout”中的新文件中
  • 请记住,在这两个布局中,为各自的组件保持相同的“id”(这就是为什么需要复制粘贴)
  • 享受它的工作方式

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

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

    还有一个类似的问题,你可以,也可以为你工作。

    改变这个

    android:windowSoftInputMode="stateUnspecified|adjustPan"
    


    只有在您的清单文件中

    对于将来遇到此问题的人,我最终只使用了一个对话框而不是弹出窗口,并且在2.2版上使用对话框平移以保持EditText在视图中正常工作。

    花了几个小时才弄清楚,我最终使用了scrollview


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

    Meh,不幸的是,没有运气。不过,谢谢!根据Android文档,这只适用于键盘的实际可用性发生变化时,而不是仅显示或隐藏键盘时。不过,他们确实提到了配置类的keyboardHidden属性,我将看一看。谢谢!
    android:windowSoftInputMode="stateUnspecified|adjustPan"
    
    android:windowSoftInputMode="adjustPan"