通过单击android中的按钮来交换两个edittext的位置

时间:2014-12-03 11:31:10

标签: android

我在另一个下面有两个edittext。我想将第一个edittext移动到第二个edittext的位置,并通过单击按钮将第二个edittext移动到第一个edittext的位置。如果再次点击该按钮,反之亦然。

2 个答案:

答案 0 :(得分:0)

试试这个:

button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText button3;
            EditText button4;
            button3 = (EditText) findViewById(R.id.button3);
            button4 = (EditText) findViewById(R.id.button4);
            int top3 = button3.getTop();
            int bot3 = button3.getBottom();
            int left3 = button3.getLeft();
            int right3 = button3.getRight();

            int top4 = button4.getTop();
            int bot4 = button4.getBottom();
            int left4 = button4.getLeft();
            int right4 = button4.getRight();

            button4.setTop(top3);
            button4.setBottom(bot3);
            button4.setLeft(left3);
            button4.setRight(right3);

            button3.setTop(top4);
            button3.setBottom(bot4);
            button3.setLeft(left4);
            button3.setRight(right4);

        }
    });

答案 1 :(得分:0)

private void exchangeButtons(Button btn1, Button btn2) {
    // Create the animation set
    AnimationSet exchangeAnimation = new AnimationSet(true);
    TranslateAnimation translate = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, btn2.getLeft(),
        Animation.RELATIVE_TO_SELF, btn1.getLeft(),
        Animation.RELATIVE_TO_SELF, btn2.getRight(),
        Animation.RELATIVE_TO_SELF, btn1.getRight());
    translate.setDuration(500);
    exchangeAnimation.addAnimation(translate);
    //int fromX = btn1.getLeft();
    //int fromY = btn1.getRight();
    //int toX = btn2.getLeft();
    //int toY = btn2.getRight();
    Log.d("ArrangeMe", 
        "view1 pos:" + btn1.getLeft() + ", 
        " +btn1.getRight() + "view2 pos:" + 
        btn2.getLeft() + ", " + btn2.getRight());
    AnimationSet exchangeAnimation1 = new AnimationSet(true);
    TranslateAnimation translate1 = new TranslateAnimation( 
        Animation.RELATIVE_TO_SELF, btn1.getLeft(),
        Animation.RELATIVE_TO_SELF, btn2.getLeft(),
        Animation.RELATIVE_TO_SELF, btn1.getRight(),
        Animation.RELATIVE_TO_SELF, btn2.getRight());
    translate1.setDuration(500);
    exchangeAnimation1.addAnimation(translate1);
    // EXECUTE btn1.startAnimation(exchangeAnimation);
    btn2.startAnimation(exchangeAnimation1);
}

有关详细信息Visit this

编辑

link可能会解决您的问题