【问题标题】:Move the button randomly on the screen without click [closed]在屏幕上随机移动按钮而不点击[关闭]
【发布时间】:2014-07-04 20:00:35
【问题描述】:

我想让一个按钮在屏幕上随机移动,而不必实现onClick 方法来移动它。我正在使用RandomsetXsetY 来为不同的活动设置按钮属性,效果很好,但我希望按钮可以使用相同的setXsetY 随机移动。有什么想法或有什么好的方法吗?

【问题讨论】:

  • 使用带有处理程序的线程

标签: android button random click


【解决方案1】:

只需将此代码添加到您的 onCreate() 中即可:

Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                              // Your logic for setting your button 
                              // randomly on the screen
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        t.start();

【讨论】:

  • 建议Thread.sleep(900 + rand.nextInt(200)); 或类似的方法来改变跳跃之间的时间。
  • @AndroidWarrior 效果很好!
  • @rossum 我目前正在尝试实现它,这是一个非常好的主意!也谢谢!
  • @androidcoderookie 不客气 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多