在 Android 中取消已经打开的 toast
我目前正在开始开发 Android 应用程序,并且一直在跟进本教程了解如何使用和改进 Google 地图应用程序。
我成功地在屏幕上显示了地图,通过触摸,我获得了某个位置的地址(通过反向地理编码),并显示了一个 Toast
。但这是我的问题 - 当您在地图上连续点击多次时,您将依次获得所有 toast
并且每个人都会花时间(在我的例子中 - Toast .LENGTH_LONG
) 消失。我想让应用程序自动关闭旧的 toast 并显示单击新地址的新 toast。
在其他资源中,我发现我应该使用 toast.cancel()
方法来实现此目的,但我在使用它时遇到麻烦 - 我已经覆盖了 onTouchEvent
- 我该如何在显示 toast
时检测到地图上有新的触摸? 或者您可能会建议我一种更好的方法来隐藏已经打开的toast
?
我尝试使我的 Toast
地址成为全局地址,但它也不起作用。
这是我的应用程序代码:
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p2 = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(p2.getLatitudeE6() / 1E6,
p2.getLongitudeE6() / 1E6, 1);
String add = " ";
if (addresses.size() > 0)
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
add += addresses.get(0).getAddressLine(i) + "\n";
Toast address;
address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
I'm currently starting to develop Android applications and I've been following up a this tutorial on how to use and improve the Google maps application.
I've managed to show up on screen the map, on touch I get the address of a location (via Reverse Geocoding) with the showing of a Toast
. But here is my problem - when you click a number of consecutive times over the map you will get all the toasts
one after other and each of them will take his time (in my case - Toast.LENGTH_LONG
) to disappear. I want to make the application automatically close the older toast and show a new toast with the new address clicked.
In other resources I found I should use the toast.cancel()
method for this purpose, but I experience trouble using it - I have already overrided the onTouchEvent
- how can I detect there is a new touch over the map while the toast
is showing?
Or maybe you would suggest me a better way of hiding the already open toast
?
I've tried to make my Toast
address a global one, but it wasn't working also.
Here is my code for the application:
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p2 = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(p2.getLatitudeE6() / 1E6,
p2.getLongitudeE6() / 1E6, 1);
String add = " ";
if (addresses.size() > 0)
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
add += addresses.get(0).getAddressLine(i) + "\n";
Toast address;
address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会将
Toast 地址
显示为全局,但每次单击时都会创建一个新的本地 Toast 对象:这将覆盖您正在创建的全局对象。我建议将
address
作为类中的private static
对象,以确保无论您单击多少次,地址始终是同一个对象,以便您始终取消您上次显示的Toast
(因为只有一个),并删除本地声明:...
You don't show where you have
Toast address
as a global, but you are creating a new, local Toast object each time you click with:Which will override the global object you are creating. I'd recommend having
address
as aprivate static
object in your class to ensure that address will always be the same object no matter how many times you click so that you are always cancelling theToast
that you last showed (since there is only ever one), and remove the local declaration:...
通过调用 make() 创建 Toast 时,您需要获取实例。之后在显示新 Toast 之前,您应该取消旧 Toast。祝您好运!
You need to get instance when create Toast by call make().After that before you show new toast, you should cancel old Toast.GoodLuck!