简体   繁体   English

如何在 Toast 中显示数组

[英]how to show array in Toast

i have an app to show a toast with specific duration time and specific time to show next toast and show randomly on display, thath's work safe but don't show array items in toast.我有一个应用程序来显示具有特定持续时间和特定时间的吐司,以显示下一个吐司并在显示器上随机显示,这是安全的,但不在吐司中显示数组项。 how do it?怎么办? tnx 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);

You can specify duration of a Toast by using您可以使用指定 Toast 的持续时间
Toast.LENGTH_LONG and Toast.LENGTH_SHORT by default in android. android 中默认为 Toast.LENGTH_LONG 和 Toast.LENGTH_SHORT。

But Toast.LENGTH_LONG duration is 1500 ms (1.5 second)但 Toast.LENGTH_LONG 持续时间为 1500 毫秒(1.5 秒)
and Toast.LENGTH_SHORT duration is 3000 ms (3 second)和 Toast.LENGTH_SHORT 持续时间为 3000 毫秒(3 秒)

1000 millisecond = 1 second. 1000 毫秒 = 1 秒。

you can use a number replacing them.你可以用一个数字代替它们。
500 for .5 second, 500 0.5 秒,
1000 for 1 second, 1000 1 秒,
1500 for 1.5 second, 1500 为 1.5 秒,
2000 for 2 second , 2000 2 秒,
2500 for 2.5 second, 2500 为 2.5 秒,
3000 fro 3 second, 3000 3 秒,
or more或更多
3500 for 3.5 second 3500 3.5 秒
as you wish.如你所愿。

In kotlin:在科特林:

 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()

In Java:在 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM