如何在Toast中显示数组

时间:2016-05-24 05:35:20

标签: java android android-toast

我有一个应用程序来显示具有特定持续时间和特定时间的吐司,以显示下一个吐司并随机显示,  这是安全的,但不要在吐司中显示数组项目。 怎么做? TNX

 //MyReceive
public void onReceive(Context con, Intent mIntent) {

    mContext = con;
    final String[] array = { "1", "3", "4", "5", "6", "ffff","END"};


        final Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                Random r = new Random();
                int i1 = r.nextInt(Activity_Main.w);

                r = new Random();
                int i2 = r.nextInt(Activity_Main.h);
                Log.d("tag : ", i1 + "   :   " + i2);

                for (String arr : array) {
                    t1 = Toast.makeText(mContext, arr, Toast.LENGTH_SHORT);

                }

                //delay in show toast duration 100ms
                Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        t1.cancel();
                    }

                }, 100);
                //random location on screen
                t1.setGravity(Gravity.TOP, i1, i2);
                t1.show();

                //delay in show next toast
                int min = 3;
                int max = 8;
                Random random = new Random();
                int d = random.nextInt(max - min + 1) + min;
                Log.d("random ", String.valueOf(d));

                mHandler.postDelayed(this, d * 1000);

            }

        }, 100);

2 个答案:

答案 0 :(得分:0)

您可以使用
指定Toast的持续时间 Android中默认为Toast.LENGTH_LONG和Toast.LENGTH_SHORT。

但Toast.LENGTH_LONG持续时间为1500毫秒(1.5秒)
和Toast.LENGTH_SHORT持续时间是3000毫秒(3秒)

1000毫秒= 1秒。

您可以使用替换它们的号码 500秒.5秒,
1000秒1秒,
1500 1.5秒,
2000年2秒,
2500秒2.5秒,
3000秒3秒,
或更多
3500 for 3.5秒
如你所愿。

答案 1 :(得分:0)

在科特林:

 var allItems = "" //used to display in the toast
 for (str in messageArray)
    {
       allItems = allItems + "\n" + str //adds a new line between items
     }
 Toast.makeText(this,allItems, Toast.LENGTH_SHORT).show()

在Java中:

String allItems = ""; //used to display in the toast

for(String str : messageArray){

    allItems = allItems + "\n" + str; //adds a new line between items

 }

  Toast.makeText(getApplicationContext(),allItems, Toast.LENGTH_LONG).show();