Cancelling an already open toast in Android

别说谁变了你拦得住时间么 提交于 2019-12-19 06:02:33

问题


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;
}

回答1:


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();



回答2:


You need to get instance when create Toast by call make().After that before you show new toast, you should cancel old Toast.GoodLuck!



来源:https://stackoverflow.com/questions/6744147/cancelling-an-already-open-toast-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!