我需要为Toast.makeText(...)创建一个类似的函数,但更快消失。当您从工具栏中选择一个按钮时,我在软件“Le monde fr”中看到了这样的消息框。如果你移动到另一个图标,它会很快出现和消失。我希望做同样的功能,但无法弄清楚如何做到这一点。 Messagebox不应该是模态的,我想要的是一种快速的工具提示。工具提示应该出现并快速消失。任何想法?
答案 0 :(得分:4)
创建Toast对象:
final Toast toast = Toast.makeText(this, "message", Toast.LENGTH_SHORT);
创建一个Timer对象:
Timer timer = new Timer();
创建取消Toast
对象的任务:
TimerTask task = new TimerTask() {
@Override
public void run() {
// make sure to cancel the Toast in UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
toast.cancel();
}
});
}
};
在指定的时间段后运行取消任务
timer.schedule(task, 100);
答案 1 :(得分:1)
工具提示可以通过PopupWindows完成。我假设你有一个图标网格,并希望提供基于时间消失或点击其他图标的工具提示 - 如果我错了,请纠正我:
创建一个全局mPopupWindow并在每次向新的mIn冒充之前解除它。您可以根据时间或在滚动侦听器中设置代码来使用为您解散它的线程。
PopupWindow mPopupWindow = null;
mIconButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
mPopupWindow = null;
}
TextView tv = new TextView(getApplicationContext());
tv.setText(tooltipText);
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,
0, 0, 0);
mPopupWindow = new PopupWindow(tv);
mPopupWindow.setWindowLayoutMode(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.showAsDropDown(v);
// write threads to disable it based on time as well
}
});
答案 2 :(得分:0)
您是否尝试过更改Toast通知的持续时间?
Toast.makeText(context, "My message", Toast.LENGTH_SHORT).show();
我会警惕显示通知并且消失得比这更快,因为短持续时间针对用户观看和阅读吐司的最短时间进行了优化。
答案 3 :(得分:0)
例如:MyCount counter = new MyCount(5000,1000);
将显示吐司5秒钟。降低值以获得所需的结果。
LayoutInflater inflater = getLayoutInflater();
查看layout = inflater.inflate(R.layout.toastxml, (ViewGroup)findViewById(R.id.toast_layout_root)); toast = new Toast(this); toast.setView(布局);
TextView text =(TextView)layout.findViewById(R.id.text); text.setText(“你好!这是一个自定义吐司!”);
MyCount counter = new MyCount(5000,1000); counter.start();
类MyCount扩展了CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
toast.cancel();
}
@Override
public void onTick(long millisUntilFinished) {
toast.show();
}
}