【问题标题】:Make system randomly choose a toast to show on click让系统随机选择一个 toast 在点击时显示
【发布时间】:2013-08-18 03:33:25
【问题描述】:

我有一个停止录制的按钮,当它被点击时,它当前会弹出一个 toast 通知。我在想,如果我可以有多个 toast 消息并且系统会随机选择(并显示)其中一个 toast,这样用户每次完成录制时都不会收到相同的消息,那会更酷一些。我不知道这是否真的可能,我只是好奇。

我的 onClick() 代码:

stopButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            /*startButton.setEnabled(true);
            stopButton.setEnabled(false); */
            recorder.stop();
            recorder.release();
            addRecordingToMediaLibrary();
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            Toast.makeText(getApplicationContext(), "Awesome Recording!", Toast.LENGTH_LONG).show();
        }
    });
}

【问题讨论】:

  • 这并没有真正显示出太多的努力。有很多方法可以做到这一点。您需要学习如何在一个范围内生成伪随机数(与您必须显示的字符串数量相匹配)并以某种方式将其交叉引用到您的字符串 xml 以提取适当的文本。 stackoverflow.com/questions/15517585/…
  • 哦,我明白了。非常感谢。抱歉,下次我会努力研究更多。向你致敬

标签: android toast


【解决方案1】:

编辑 2

我的错误,new Random().nextInt(...) 行多次返回相同的整数,因为 Java 中的随机数生成器实际上是伪随机的;它使用种子生成随机值,并在每次重置种子时创建一个新的Random 对象,从而导致重复整数。尝试将此字段添加到您的匿名OnClickListener

private static final Random random = new Random();

改变

new Random().nextInt(toastMessages.length - 1);

random.nextInt(toastMessages.length - 1);

所以你最终得到以下结果:

stopButton.setOnClickListener(new OnClickListener() {
    private static final Random random = new Random();

    @Override
    public void onClick(View v) {
        recorder.stop();
        recorder.release();
        addRecordingToMediaLibrary();
        startButton.setEnabled(true);
        stopButton.setEnabled(false);

        String[] toastMessages = new String[] {"Great!", "Awesome!", "..."};
        int randomMsgIndex = random.nextInt(toastMessages.length - 1);
        Toast.makeText(getApplicationContext(), toastMessages[randomMsgIndex], Toast.LENGTH_LONG).show();
    }
});

此解决方案并非特定于 Android,但您可以使用一组预定义消息(最好从 strings.xml 加载这些消息):

String[] toastMessages = new String[] {"Great!", "Awesome!", "..."};

然后从该数组中随机选择一个索引:

new Random().nextInt(toastMessages.length - 1);

给你:

String[] toastMessages = new String[] {"Great!", "Awesome!", "..."};

// Get an index between 0 and the last index in the messages array 
int randomMsgIndex = new Random().nextInt(toastMessages.length - 1);

Toast.makeText(getApplicationContext(), toastMessages[randomMsgIndex], Toast.LENGTH_LONG).show()

编辑:Eclipse 应该为您处理这个问题,但只是为了确保:

import java.util.Random;

【讨论】:

  • 好的,所以我尝试了这个,它确实打印出数组中的一个字符串,但它只打印第一个。它从不打印任何其他内容。我该如何解决这个问题?
  • 那是我的一个错误。请检查更新的解决方案。
  • 谢谢!这正是我想要的,而且效果很好!
【解决方案2】:

当然可以。我能想到的最简单的方法是,如果要显示一定数量的消息,只需创建一个 String Array 来保存不同的消息。然后在onClick() 中只需使用随机数生成器从Array 中选择一条消息。

很喜欢

Random randomNumber = new Random();
int number = randomNumber.nextInt(5); // where 5 is the number of Strings minus 1
Toast.makeText(YourActivityName.this, YourStringArray[number], Toast.LENGTH_LONG).show();

【讨论】:

  • 这很好用,感谢codeMagic!但由于某种原因,它只打印其中一个字符串。它永远不会转到另一个字符串?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
相关资源
最近更新 更多