2015-08-25 28 views
2

我正在创建一个拼图比赛游戏,为了匹配两个按钮,你必须交换他们的位置。例如,我想将button1的位置与button2所在的位置交换。任何人都有什么建议?如何交换android中两个按钮的位置?

这里是我下面的代码:

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import java.util.ArrayList; 
import java.util.Collections; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 

import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 


public class puzzle extends ActionBarActivity { 
    private TextView moveCounter; 
    private TextView feedbackText; 
    private Button[] buttons; 
    private Boolean bad_move=false; 
    // private static final Integer[] goal = new Integer[] {0,1,2,3,4,5,6,7,8}; 
    // private String [][] puz = new String [3][3]; 
    Button b [][]; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_puzzle); 



     swap(); 

    } 
    private void setBoard(){ 
     b = new Button[3][3]; 

     b[0][0]= (Button).findViewById(R.id.button1); 
     b[0][1]= (Button).findViewById(R.id.button2); 
     b[0][2] = (Button).FindById(R.id.button3); 
     b[1][0] = (Button).FindBdyId(R.id.button4); 
     b[1][1] = (Button).FindById(R.id.button5); 
     b[1][2] = (Button).FindById(R.id.button6); 
     b[2][0] = (Button).FindById(R.id.button7); 
     b[2][1] = (Button).FindById(R.id.button8); 
     b[2][2] = (Button).FindById(R.id.button9); 

    } 
    private void swap() { 
     b.setOnClickListener(new View.OnClickListener(){ 


      @Override 
      public void onClick(View v) { 

      } 
     }); 


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_puzzle, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

你试过什么?在这里你只需要引用按钮,而没有更多。 –

+0

将对这两个按钮的引用互换,或将父对象列表中的引用彼此交换,而不是使父代布局无效。通过编写一个从LinearLayout,RelativeLayout等扩展的简单自定义ViewGroup类,这可能会更容易... –

+0

我打算使用一个2d数组,将一个按钮定位在这个索引处,并有一个if语句将它与下一个按钮。 – user3261680

回答

0

如果您使用的API级别大于11,你可以使用的getX()和的getY()来获得一个按钮的坐标。

和setX()和setY()在特定坐标中设置按钮。

例如,

// getting coordinates of button 
float posX = button.getX(); 
float posY = button.getY(); 

// setting button2 in the coordinates that we got above 
button2.setX(posX); 
button2.setY(posY); 
+0

好的!感谢您的帮助。我会尝试一下。 – user3261680

+0

你试过了吗? – Ash

+0

还没有。但我正在做更多的研究。 – user3261680

0

我已经得到这个工作的方式类似你的方法,你原先公布。试试这个镜头:

Button btnOne = (Button) findViewById(R.id.buttonOne); 
Button btnTwo = (Button) findViewById(R.id.buttonTwo); 

float posOneX = btnOne.getX(); 
float posOneY = btnOne.getY(); 

float posTwoX = btnTwo.getX(); 
float posTwoY = btnTwo.getY(); 

btnOne.setX(posTwoX); 
btnOne.setY(posTwoY); 

btnTwo.setX(posOneX); 
btnTwo.setY(posOneY);