【问题标题】:How to make android edittext fields visible when keyboard is open?键盘打开时如何使android edittext字段可见?
【发布时间】:2013-12-02 03:07:18
【问题描述】:

在我的 android 应用程序中,在登录页面上,有两个 edittext 字段用户名、密码。问题是当我点击密码时,键盘弹出,然后大部分密码字段被键盘覆盖。

我希望基本上有一个动画,当键盘弹出时(平滑地)向上移动两个字段,当键盘消失时,字段应该平滑地向下移动。有谁知道我该怎么做?

我还想避免放置固定数量的像素来移动,因为那样它将取决于设备。如果可能的话,可以使用类似 ems 单位的东西,所以它适用于所有屏幕密度。

谢谢

【问题讨论】:

  • 您是否尝试将 android:windowSoftInputMode="adjustResize" 添加到清单中登录页面的活动条目中?

标签: android android-edittext android-animation android-softkeyboard


【解决方案1】:

为此,您必须实现 *addOnGlobalLayoutLitener。使用 scrollview 包裹整个子视图,然后滚动布局。通过更改滚动值来测试您的应用。 *

final View activityRootView = findViewById(R.id.signinRootView);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int heightDiff = activityRootView.getRootView()
                                .getHeight() - activityRootView.getHeight();
                        if (heightDiff > 100) {
                            ((ScrollView) findViewById(R.id.scrollview))
                                    .scrollTo(0, findViewById(R.id.et_password)
                                            .getBottom() + 80);
                        } else
                            ((ScrollView) findViewById(R.id.scrollview))
                                    .scrollTo(0, 0);
                    }
                });

**

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多