如何在警报对话框上自动添加逗号

时间:2018-10-24 03:36:18

标签: java android-studio

$file_name = preg_replace('/([^\x20-~]+)|([\\/:?"<>|]+)/g', '_', $client_specified_file_name);

大家好,谢谢您拨冗阅读我的第一篇文章 我正在做有关在警报对话框中的文本上自动添加逗号的家庭作业。 这是应用程序的外观

the "UI" this is where the alert dialogue take the information

Here is the example when submit button is pressed

现在的问题是如何在爱好文本之间添加逗号?

之前(图片中的结果)

爱好:Sepak Bola主游戏Makan

没有逗号分隔爱好

在此情况下,当选中2个或更多复选框时,警报对话框的外观将自动显示

业余爱好:Sepak Bola,主游戏,马坎    要么    兴趣爱好:Sepak Bola,马坎

如何添加此逗号?无需手动输入...(“,” ..在代码中

对不起,如果已经讨论过此类问题,我真的不知道要在google或StackOverflow上搜索哪个关键字

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用番石榴连接器来连接文本。但是我不知道您用来保存此数据的数据结构。如果您使用列表来保存兴趣爱好,那么您可以像下面这样

List<String> hobbies= Lists.newArrayList("Sepak Bola", " Main Game", "Makan");
String result = Joiner.on(",").join(hobbies);

Tim的修改:我们可以将上述方法与您的实际对话代码结合使用:

List<String> hobbies = Lists.newArrayList(cbBola, cbGame, cbMakan);
String result = Joiner.on(",").join(hobbies);

hasilInput.setMessage("Nama : " + txtNama.getText().toString() + "\n" +
                      "Kelas : " + txtKelas.getText().toString() + "\n" +
                      "Kamu Seorang " + radBut1.getText() + "\n" +
                      "Hobby : " + result  + "\n")
    .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
});