简体   繁体   中英

How do I make my buttons show up in random places until the button is pressed?

I have tried basically everything! I am trying to make a short mini-game where you have to press the image that pops up on the screen. The image moves to different points of the application randomly, but the problem is I cannot continue moving the image around! The image is a button by the way!

GameView.java

package interactive.siddiqui.fortuneteller;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.Random;


public class GameView extends Activity{

private Button btn;


private boolean tf = false;

private final int SPLASH_DISPLAY_LENGTH = 500;



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

    boolean foo = true;
    while(foo = true) Click();
}


@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_game_view, menu);
    return true;
}

public void Click() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Button btn = (Button) findViewById(R.id.button9);
            Random r = new Random();

            int x = r.nextInt(480);
            int y = r.nextInt(800);

            btn.setX(x);
            btn.setY(y);




        }
    }, SPLASH_DISPLAY_LENGTH);

}

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




public void bob(View view){
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(GameView.this, Finished.class);
            startActivity(intent);
            tf = true;
        }
    });
}
}

This mainly applies to Java, but I will add in the corresponding XML file too.

XML File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"      
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin"
 tools:context="interactive.siddiqui.fortuneteller.GameView"
 android:background="@drawable/background_fortune">

 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text=""
     android:id="@+id/button9"
     android:layout_centerVertical="true"
     android:layout_centerHorizontal="true"
     android:background="@drawable/small_fortune"
     android:onClick="bob"
     android:layout_x = "0dp"
     android:layout_y = "0dp"/>
</RelativeLayout>

How do I do this? I just want the button to show up in random places until the button is clicked! THIS IS NOT HOMEWORK, by the way...

Instead of using a infinite while, which locks the UI thread (so you won't see any updates to the position), use a Timer and a TimerTask.

Also, your bob method, you were assigning a new click listener, which I assume t wasn't what you actually wanted.

Anyway, try this and if you have any doubt about the code, just ask :)

package interactive.siddiqui.fortuneteller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class GameView extends Activity{

    private Button btn;


    private boolean tf = false;
    private boolean canMove = true;
    private Timer timer;

    private final long SPLASH_DISPLAY_LENGTH = 500;


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

        start();
    }


    @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_game_view, menu);
        return true;
    }

    public void start()
    {
        canMove = true;
        timer = new Timer();
        timer.scheduleAtFixedRate(
                new TimerTask()
                {
                    @Override
                    public void run()
                    {
                        moveButton();
                    }
                },
                SPLASH_DISPLAY_LENGTH,
                SPLASH_DISPLAY_LENGTH
        );
    }

    private void moveButton()
    {
        if(!canMove){ return; }

        runOnUiThread(
                 new Runnable()
                 {
                     @Override
                     public void run()
                     {
                        Button btn = (Button)findViewById(R.id.button9);
                        Random r = new Random();

                        int x = r.nextInt(480);
                        int y = r.nextInt(800);

                        btn.setX(x);
                        btn.setY(y);
                    }
                }
        );
    }

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




    public void bob(View view)
    {
        canMove = false;

        if(timer != null)
        {
            try
            {
                timer.cancel();
                timer.purge();
            }catch(Exception e){ e.printStackTrace(); }

            timer = null;
        }

        Intent intent = new Intent(GameView.this, Finished.class);
        startActivity(intent);
        tf = true;
    }
}

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