【问题标题】:How to avoid cancellation of animator effect when soft keyboard appears?如何避免出现软键盘时取消动画效果?
【发布时间】:2015-12-02 07:11:42
【问题描述】:

在我的应用程序中,一些视图由 Animator 调整大小,一切正常,直到出现软键盘。当我在动画完成后点击EditText 时,EditText 的高度会在软键盘出现之前回到 xml 高度。

详情: 假设我们有一个简单的布局,只有一个按钮和一个文本编辑框:

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

    <Button
        android:id="@+id/button"
        android:text="start anim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/text"
        android:layout_below="@id/button"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:text="Hello World!" />
</RelativeLayout>

还有简单的动画师:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:propertyName="bottom"
    android:duration="500"
    android:valueTo="200dp"
    android:valueFrom="56dp"
    android:repeatCount="0"
    android:valueType="intType"
    />
</set>

然后当点击按钮上的事件时,我运行动画:

final EditText editText = (EditText) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AnimatorSet show = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.expand_edit_text);
        show.setTarget(editText);
        show.start();
    }
});

到目前为止还可以。但是当我点击EditText 并出现软键盘时,EditText 会回到之前的高度。 如何避免这种影响?并使动画持久化/永久化?

我发现类似问题没有回复Android showing keyboard cancels animation

【问题讨论】:

    标签: android android-animation android-softkeyboard


    【解决方案1】:

    拨打clearAnimation() 拨打您拨打startAnimation() 的任何View

    如何检查键盘可见性:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks whether a hardware keyboard is available
        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)
        {
            Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
            Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

    • 我试过了,但没有用。我希望动画效果将是动画持久/永久的。而且我不明白 clearAnimation() 是怎么做到的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多