android - Android 中使用小动画更改 EditText 的文本

标签 android android-edittext

我正在为 Android 制作一个简单的计算器应用程序。我已经编写了代码并且一切正常。但是,我想通过添加一些用于更改文本的动画来改进其用户界面的设计。例如,它是这样的:

  1. 我将通过按钮在编辑文本中输入内容,例如“2+3”。
  2. 当我按下回车按钮时,“2+3”向上移动并消失,然后“5”从下面开始取代“2+3”的原始位置。

我该怎么做?

最佳答案

现在我对它有了更好的理解,这应该可以回答你的问题。您只需要几个动画文件,一个用于将文本滑出,一个用于将文本滑入。您可以根据需要调整动画:

这是等于按钮的点击监听器:

    Button btnEquals = (Button)findViewById(R.id.btnEquals);
        btnEquals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Animation slideOutBottom = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.out_bottom);
                slideOutBottom.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
// Update the text here
                        Animation slideInTop = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.in_top);
                        llContainer.startAnimation(slideInTop);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                llContainer.startAnimation(slideOutBottom);

            }
        });
    }

这里是 anim 文件夹中的 out_bottom.xml 和 in_top.xml 文件(分别):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0%" android:toYDelta="100%" android:duration="600"/>
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="600"/>
</set>

这是我如何包装布局的。有点快速和肮脏,但它有效:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:background="#fff">

        <LinearLayout
            android:id="@+id/llContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <EditText
                android:id="@+id/edtInput"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="top"
                android:padding="5dp"
                android:background="@null"/>

        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/btnEquals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="/>

</LinearLayout>

关于android - Android 中使用小动画更改 EditText 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30581008/

相关文章:

java - cordova平台添加android在JAVA_HOME中出现错误

android - findViewById() 在一个 Activity 中有效,但在另一个 Activity 中无效

java - 自定义键盘 : handling inputType change

android - 从保存的实例恢复 fragment 时如何防止 editText 自动填充文本?

Android 音频延迟解决方法

java - 已解决 - Java ShakeDetector 多次触发 - 如何获取最后的输出?

android - 如何使EditText粗体斜体下划线android

android - 由 EditText 组成的自定义 ListAdapter 失去焦点调用两次

Android:如何禁止在扫描 NFC 标签时重新打开您的应用程序?

java - 不在 Activity 之间传递的 Parcelable 数据