[英]How to retrieve String array from Webservice and show in toast in android
[英]how to show array in 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);
您可以使用指定 Toast 的持续时间
android 中默认为 Toast.LENGTH_LONG 和 Toast.LENGTH_SHORT。
但 Toast.LENGTH_LONG 持续时间为 1500 毫秒(1.5 秒)
和 Toast.LENGTH_SHORT 持续时间为 3000 毫秒(3 秒)
1000 毫秒 = 1 秒。
你可以用一个数字代替它们。
500 0.5 秒,
1000 1 秒,
1500 为 1.5 秒,
2000 2 秒,
2500 为 2.5 秒,
3000 3 秒,
或更多
3500 3.5 秒
如你所愿。
在科特林:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.