0

当屏幕足够高但不包括短屏幕上的图像时,我想创建一个动态登录屏幕,其中包含装饰性品牌图像。当键盘出现时,可能需要删除图像。当键盘被隐藏时,图像可以回来。

对于网页,我只需对设备高度使用 CSS 媒体查询,适当地显示或隐藏图像,这一切都会很好地工作。我认为 Android 视图不可能有任何简单和干净的东西,是吗?所以我想我需要知道创建活动时窗口的高度并适当地创建视图。

在清单中,我将我的主要活动设置adjustResize为键盘出现的时间。当键盘出现时,我的视图确实会调整大小,但令人惊讶的是我的活动没有重新创建。当屏幕旋转时,将重新创建活动。

文档说,当键盘可用性发生变化时,将重新创建视图。第一段来自https://developer.android.com/guide/topics/resources/runtime-changes

某些设备配置可能会在运行时更改(例如屏幕方向、键盘可用性以及用户启用多窗口模式时)。当发生这种变化时,Android 会重新启动正在运行的 Activity(调用 onDestroy(),然后调用 onCreate())。重新启动行为旨在通过使用与新设备配置匹配的替代资源自动重新加载您的应用程序来帮助您的应用程序适应新配置。

我的问题是

处理我的设计目标的最佳方式是什么?

为什么键盘出现时我的活动没有重新创建?

以下是我的测试应用程序的相关部分。这没有图像,因为在遇到与文档相矛盾的行为之前,我什至没有走得那么远。

AndroidManifest.xml

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("MainActivity", "onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Top"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorAccent" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/intro"
        android:singleLine="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Bottom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@color/colorAccent" />

</android.support.constraint.ConstraintLayout>
4

4 回答 4

1

该方法OnFocusChangeListener()将检测何时View获得或失去焦点。EditText使键盘在获得焦点时出现。因此,您应该将 a 附加OnFocusChangeListener()到您想要的那些EditText

EditText myET = (EditText)findViewById(R.id.myET);

myET.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override          
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
            //Hide image
        } else {
            //Reveal image
        }
    }
});

此外,要隐藏和显示图像,您应该使用名为visibility. 方法是:

myImageView.setVisibility(View.VISIBLE); //This will show the Image
myImageView.setVisibility(View.INVISIBLE); //This will make the Image invisible
myImageView.setVisibility(View.GONE); //This will make the Image invisible, and also it will collapse the layout, occupying no space at all.

不要尝试同时使用INVISIBLEGONE,因为这可能会造成一些麻烦。就您而言,据我了解,您可能想要使用GONEand VISIBLE

这种方法的唯一问题是,如果您有多个 EditText,则必须设置多次相同的代码。要解决这个问题,请参考以下链接: EditText setOnFocusChangeListener on all EditTexts

于 2019-01-25T04:30:15.383 回答
1

出现键盘时不会重新创建活动,因为这不是 Android 的工作方式。它不应该是。(尽管如果您有一个带有滑出物理键盘的老式设备,当您滑出它时会重新创建它,因为它被视为硬件配置更改)。显示/隐藏键盘是在没有娱乐的情况下完成的。这是一件好事,考虑到有多少人只是将大量逻辑推到 onCreate 中,许多重新创建事件会很昂贵。

如何做你想做的事——你做不到。没有 API 可以检测键盘何时打开。有一些常用的黑客试图发现它,但它们都有缺陷(它们可能在分屏模式、画中画模式、多屏幕和太小的键盘方面存在问题,因为它们都是基于猜测工作的关于高度变化)。

于 2019-01-25T03:25:48.703 回答
0

我一直在寻找答案,我遇到了这个:

https://developer.android.com/training/keyboard-input/visibility#java

它说android确实做你想做的事。

我刚刚尝试过,在您的清单中添加 1 行 ONE LINE! 并且它可以工作。

哦等等……你想让这个品牌消失……嗯,这不是必须的吗?

哦!你已经知道这件事了......

于 2019-01-26T03:28:42.163 回答
-2

"What you want to do you can't"... I don't know how to do it, but I disagree that it can't be done. I have the PayPal app and when the keyboard appears the login button and the rest of the login screen resize so that the login button isn't covered by the keyboard. How they're doing it, I don't know, but obviously somehow the app knows the keyboard appears and adjusts accordingly.

I've been searching for an answer and I came across this:

https://developer.android.com/training/keyboard-input/visibility#java

It says android DOES resize the screen when then soft keyboard appears/disappears.

I just tried it, add 1 line, ONE LINE!, to your manifest and it works.

于 2019-01-25T03:42:58.717 回答