取消一个已打开的烤面包的Andr​​oid

编程入门 行业动态 更新时间:2024-10-25 20:27:38
本文介绍了取消一个已打开的烤面包的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

本文发布于:2023-10-16 11:46:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烤面包   Andr   oid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!