取消一个已打开的烤面包的Andr​​oid [英] Cancelling an already open toast in Android

查看:125
本文介绍了取消一个已打开的烤面包的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我开始开发Android应用程序,我一直在跟进一个的关于如何使用和改进谷歌地图应用程序本教程

我已经成功地显示在屏幕上的地图,触摸我得到一个位置(通过反向地理编码)的地址与一个放映吐司。但这里是我的问题 - 当你点击了一批连续多次在地图上,你会得到所有的祝酒词另外一个,每个人都会把他的时间之后(在我案例 - Toast.LENGTH_LONG )消失。我想使应用程序会自动关闭旧的敬酒,并显示一个新的烤面包,新地址点击。

在其他资源,我发现我应该使用 toast.cancel()方法用于此目的,但我遇到麻烦了使用它 - 我已经overrided的的onTouchEvent - 我怎么能发现有一个新的触摸在地图上,而面包是显示? 或者,也许你会建议我躲在已经打开敬酒一个更好的方式

我试图让我的吐司应对全球性的,但它不工作也。

下面是我的code应用程序:

  @覆盖
公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面){
    // ---当用户抬起手指---
    如果(event.getAction()== 1){
        。的GeoPoint P2 = MapView.getProjection()在fromPixels((int)的event.getX(),(int)的event.getY());
        地理codeR地理codeR =新的地缘codeR(getBaseContext(),Locale.getDefault());
        尝试 {
            名单<地址>地址=地理coder.getFromLocation(p2.getLatitudeE6()/ 1E6,
                    p2.getLongitudeE6()/ 1E6,1);
            字符串添加=「;
            如果(addresses.size()大于0)
                的for(int i = 0; I< addresses.get(0).getMaxAddressLineIndex();我++)
                    添加+ = addresses.get(0).getAddressLine(我)+\ N的;
            吐司地址;
            地址= Toast.makeText(getBaseContext(),添加,Toast.LENGTH_LONG);
            address.show();
        }
        赶上(IOException异常E){
            e.printStackTrace();
        }
        返回true;
    }
    返回false;
}
 

解决方案

你没有表现,你有吐司地址作为一个全球性的,但你要创建一个新的,当地土司每次对象单击有:

 吐司地址;
地址= Toast.makeText(getBaseContext(),添加,Toast.LENGTH_LONG);
address.show();
 

这将覆盖正在创建的全局对象。我建议有地址私有静态对象类,以确保该地址将始终是相同的对象不管你有多少次点击,这样你总是取消吐司您上次显示(因为只有过一个),并删除本地声明:

 私有静态吐司地址;
 

...

 如果(地址!= NULL)
    address.cancel();

地址= Toast.makeText(getBaseContext(),添加,Toast.LENGTH_LONG);
address.show();
 

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

解决方案

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

这篇关于取消一个已打开的烤面包的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆