在 Android 中取消已经打开的 toast

发布于 2024-11-25 03:27:01 字数 1691 浏览 0 评论 0原文

我目前正在开始开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-12-02 03:27:01

您不会将 Toast 地址 显示为全局,但每次单击时都会创建一个新的本地 Toast 对象:

Toast address;
address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();

这将覆盖您正在创建的全局对象。我建议将 address 作为类中的 private static 对象,以确保无论您单击多少次,地址始终是同一个对象,以便您始终取消您上次显示的 Toast (因为只有一个),并删除本地声明:

private static Toast address;

...

if (address != null)
    address.cancel();

address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();

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:

Toast address;
address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();

Which will override the global object you are creating. I'd recommend having address as a private 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 the Toast that you last showed (since there is only ever one), and remove the local declaration:

private static Toast address;

...

if (address != null)
    address.cancel();

address = Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG);
address.show();
情定在深秋 2024-12-02 03:27:01

通过调用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文