首页 文章

软键盘出现时如何避免取消动画效果?

提问于
浏览
1

在我的应用程序中,一些视图由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();
    }
});

直到现在还可以 . 但是 when I tap on EditText and soft keyboard appears then EditText backs to its previous height . 如何避免这种影响?并使动画持久/永久?

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

1 回答

  • 0

    在你叫 startAnimation()View 上调用 clearAnimation() .

    如何检查键盘可见性:

    @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();
        }
    }
    

相关问题