简体   繁体   中英

How to swap the position of two buttons in android?

I am creating aa puzzle match game where in order to match two buttons together you must swap the position of them. for example, i want to swap the position of button1 with the position that button2 is in. Anybody have any suggestions?

Here is my code below:

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

If you are using API level greater than 11, you can use getX() and getY() to get coordinates of a button.

And setX() and setY() to set a button in a particular coordinate.

for example,

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

The way I've gotten this work is similar to your approach you had originally posted. Try giving this a shot:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM