如何在Android中显示吐司? [英] How to display Toast in Android?

查看:197
本文介绍了如何在Android中显示吐司?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滑块,可以拉高,然后它显示的地图。我可以移动滑块上下隐藏或显示地图。当在地图上的面前,我可以处理该地图触摸事件。每次我碰,一个AsyncTask的被触发时,它会下载一些数据,并显示该数据的面包。虽然我开始触摸事件,显示没有面包,没有,直到我关闭滑盖的任务。当滑块是封闭的,不显示在地图中不再出现吐司

I have a slider that can be pulled up and then it shows a map. I can move the slider up and down to hide or show the map. When the map is on front, I can handle touch events on that map. Everytime I touch, a AsyncTask is fired up, it downloads some data and makes a Toast that displays the data. Although I start the task on touch event no toast is displayed, not till I close the slider. When the slider is closed and the map is not displayed anymore the Toast appears.

什么想法?

以及启动任务

编辑:

public boolean onTouchEvent(MotionEvent event, MapView mapView){ 
    if (event.getAction() == 1) {
        new TestTask(this).execute();
        return true;            
    }else{
        return false;
    }
 }

和在onPostExecute敬酒

and in onPostExecute make a toast

Toast.makeText(app.getBaseContext(),(String)data.result, 
                Toast.LENGTH_SHORT).show();

在新TestTask(本),这是一个参照MapOverlay而不是MapActivity,所以这是问题。

In new TestTask(this), this is a reference to MapOverlay and not to MapActivity, so this was the problem.

推荐答案

为了在您的应用程序显示吐司,试试这个:

How to display Toast in Android?

In order to "display Toast" in your application, try this:

Toast.makeText(getApplicationContext(), (String)data.result, 
   Toast.LENGTH_LONG).show();

另一个例子......

other example...

Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
   Toast.LENGTH_LONG).show();

我们可以定义两个constats持续时间:

We can define two constats for duration:

INT LENGTH_LONG 显示视图或文字通知长时间   时间。

int LENGTH_LONG Show the view or text notification for a long period of time.

INT LENGTH_SHORT 显示视图或文字通知在短期内   时间。

int LENGTH_SHORT Show the view or text notification for a short period of time.

了解更多关于敬酒

其他选项,自定义你的面包:

other option, customizing your toast:

LayoutInflater myInflater=LayoutInflater.from(this);
View view=myInflater.inflate(R.layout.your_custom_layout,null);
Toast mytoast=new Toast(this);
mytoast.setView(view);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();

这篇关于如何在Android中显示吐司?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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