小编典典

软键盘在PopupWindow中包含EditText

java

我汇总了一个简单的测试项目,该项目显示了一个包含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设备上仍然无法正常运行,这更加令人困惑。


阅读 637

收藏
2020-12-03

共1个答案

小编典典

对于将来遇到任何麻烦的人,我最终只使用了一个Dialog而不是PopupWindow,并通过Dialog进行平移以使EditText保持可见。

2020-12-03