2014-12-03 123 views
0

我有兩個edittext在另一個之下。我想將第一個edittext移動到第二個位置,並通過單擊按鈕將第二個edittext移動到第一個edittext的位置。如果再次點擊該按鈕,反之亦然。通過單擊android中的按鈕來交換兩個edittext的位置

+2

具有u嘗試新鮮事物? – 2014-12-03 11:32:11

+1

怎樣才能交換他們的文本? (將EditText2中的text1和EditText1中的text2) – Rami 2014-12-03 11:39:16

+0

在您的問題中添加代碼引用。 – 2014-12-03 11:43:33

回答

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); 

     } 
    }); 
+0

沒有爲我工作 – 2017-03-08 11:27:57

+0

您是否收到任何錯誤? – 2017-03-15 08:53:02

+0

它只是不起作用。總之我找到了解決這個問題的更好方法。 – 2017-03-15 11:31:57

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可以解決你的問題

相關問題