【发布时间】:2011-08-30 23:44:07
【问题描述】:
我想显示的 toast 少于 Toast.LENGTH_SHORT,因为我觉得它需要大约 2 秒。我只想显示半秒钟的吐司。
Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG 的时间间隔是多少?
【问题讨论】:
我想显示的 toast 少于 Toast.LENGTH_SHORT,因为我觉得它需要大约 2 秒。我只想显示半秒钟的吐司。
Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG 的时间间隔是多少?
【问题讨论】:
这对我有用
final Toast toast = Toast.makeText(getApplicationContext(), "The following message will disappear in half second", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);
【讨论】:
只有两个可能的值:
private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds
设置其他值不起作用。如果持续时间不等于 1 (Toast.LENGTH_LONG),则持续时间将为 SHORT_DELAY(2 秒):
long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
在Toast的来源中写道
这个时间可以由用户定义。
但我找不到这样做的方法。
更新:这里有解决办法:Set Toast Appear Length
【讨论】:
请参阅我建议的解决方案here。您基本上在比标准吐司持续时间短的指定延迟后调用 toast.cancel() 。
【讨论】:
试试这个
final Toast toast = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
toast.show();
new CountDownTimer(10000, 1000)
{
public void onTick(long millisUntilFinished) {toast.show();}
public void onFinish() {toast.cancel();}
}.start();
希望对您有所帮助.. 享受..!!!
【讨论】:
对于菜鸟,我做了最简单的解决方案——一种方法。
public void showToastMessage(String text, int duration){
final Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, duration);
}
您还应该导入:
import android.os.Handler;
import android.widget.Toast;
你可以调用这个方法例如:
showToastMessage("your noob", 1000);
上面的方法应该只在 Fragment 中有效!如果您希望它在 Activity 中工作,请将 toast 消息中的 getActivity() 替换为 getApplicationContext()。祝开发者好运!
【讨论】:
它会起作用的。
public void toastMessage(final String message) {
this.runOnUiThread(new Runnable() {
public void run() {
LayoutInflater myInflator = getLayoutInflater();
View myLayout = myInflator.inflate(R.layout.custom_layout,
(ViewGroup) findViewById(R.id.toastlayout));
TextView myMessage = (TextView) myLayout
.findViewById(R.id.label);
myMessage.setText(message);
Toast toast = new Toast(getApplicationContext());
toast.setView(myLayout);
toast.setDuration(100);
myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
toast.show();
}
});
}
【讨论】:
public void showMyToast(final Toast toast, final int delay) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 1000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, delay);
}
//如何使用(MainActivity.this为示例,可自行更改) 和(在 showMyToast(first param,second param) 方法中,第二个参数是你真正定义的时间 这个)
Toast toast=Toast.makeText(MainActivity.this, "MyToast Test", Toast.LENGTH_LONG);
showMyToast(toast, 1000);
【讨论】:
这似乎对我有用(将持续时间设置为您想要的任何内容):
mActivity.runOnUiThread(new Runnable() {
public void run() {
Toast toast = new Toast(mActivity
.getApplicationContext());
toast.setView(layout);
toast.setDuration(400);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
【讨论】: