在 Android 中使用小动画更改 EditText 的文本
Changing Text of EditText With a Little Animation in Android
我正在为 android 制作一个简单的计算器应用程序。我已经编写了代码,一切正常。但是,我想通过添加一点动画来更改文本来改进其用户界面的设计。所以它是这样的,例如:
- 我将通过按钮输入编辑文本,例如“2+3”。
- 当我按下回车键时,“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);
}
});
}
这是你的动画文件夹中的 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 制作一个简单的计算器应用程序。我已经编写了代码,一切正常。但是,我想通过添加一点动画来更改文本来改进其用户界面的设计。所以它是这样的,例如:
- 我将通过按钮输入编辑文本,例如“2+3”。
- 当我按下回车键时,“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);
}
});
}
这是你的动画文件夹中的 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>