简体   繁体   中英

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

 //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.LENGTH_LONG and Toast.LENGTH_SHORT by default in android.

But Toast.LENGTH_LONG duration is 1500 ms (1.5 second)
and Toast.LENGTH_SHORT duration is 3000 ms (3 second)

1000 millisecond = 1 second.

you can use a number replacing them.
500 for .5 second,
1000 for 1 second,
1500 for 1.5 second,
2000 for 2 second ,
2500 for 2.5 second,
3000 fro 3 second,
or more
3500 for 3.5 second
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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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