当我按下开关按钮时,如何切换像谷歌翻译这样的编辑文本?

时间:2016-12-20 06:55:53

标签: android android-edittext android-animation

是否可以使用动画

进行此操作

我在谷歌textview switcher找到了,但我想切换edittext

我想这样做:

enter image description here

提前感谢。

1 个答案:

答案 0 :(得分:2)

可能这可以帮到你,..

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="2"
    tools:context="com.app.edittextswitch.MainActivity">


    <EditText
        android:id="@+id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:drawable/editbox_background_normal" />

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/arrow" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:drawable/editbox_background_normal" />


</LinearLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    EditText edit1, edit2;
    ImageView imgView;
    Animation slide_in_left, slide_out_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgView = (ImageView) findViewById(R.id.imgView);
        edit1 = (EditText) findViewById(R.id.edit1);
        edit2 = (EditText) findViewById(R.id.edit2);

        slide_in_left = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left);
        slide_out_right = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right);


        imgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                String text1 = edit1.getText().toString();
                String text2 = edit2.getText().toString();

                edit1.setText(text2);
                edit2.setText(text1);

                edit1.startAnimation(slide_out_right);
                edit2.startAnimation(slide_out_right);

            }
        });
    }


}