java - 在按下按钮之前,如何让我的按钮出现在随机位置?

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

我该怎么做?我只想让按钮出现在随机位置,直到单击按钮为止!顺便说一下,这不是家庭作业......

最佳答案

不要使用无限 while,它会锁定 UI 线程(因此您不会看到位置的任何更新),而是使用 Timer 和 TimerTask。

此外,您的 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;
    }
}

关于java - 在按下按钮之前,如何让我的按钮出现在随机位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31716152/

相关文章:

java - 使用 Apache Lucene 删除磁盘中的所有索引数据/文件?

java - 使用构造函数注入(inject)时如何避免循环依赖

java - 如何在 Java Spring 中的 HTTP 请求中仅发送原始值

java - 让 adMob 为 Android 旧版本运行

android - 如何在项目 gradle.properties 中设置 org.gradle.jvmargs=-Xmx5120M?

android - 当应用程序处于后台或关闭状态时,无法将记录从 Firebase 消息服务插入 SQLite 数据库

java - 任何方式只忽略 "connection reset by peer"IOExceptions

java - 在 Spring Boot 服务中的自定义约束 validator 之前,Json 消息解析失败

android - 在 Android 上获取背景中的像素颜色

php - 如何在php json编码后删除隐藏的垃圾字符