在按下按钮之前,如何使按钮显示在随机位置?

时间:2015-07-30 05:59:08

标签: java android

我基本上都尝试了一切!我想制作一个简短的迷你游戏,你必须按下屏幕上弹出的图像。图像随机移动到应用程序的不同点,但问题是我无法继续移动图像!顺便说一下,图像是一个按钮!

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

这主要适用于Java,但我也会添加相应的XML文件。

XML文件

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

我该怎么做?我只是希望按钮显示在随机位置,直到单击按钮!顺便说一句,这不是家庭作业......

1 个答案:

答案 0 :(得分:1)

使用Timer和TimerTask,而不是使用无限时间来锁定UI线程(这样你就不会看到对位置的任何更新)。

此外,您的bob方法,您正在分配一个新的点击监听器,我认为这不是您真正想要的。

无论如何,试试这个,如果您对代码有任何疑问,请问:)

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