Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

android - 如何在Android中显示Toast?(How to display Toast in Android?)

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.

(每次触摸时,都会触发AsyncTask ,它会下载一些数据并制作一个Toast显示数据。)

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.

(当关闭滑块并且不再显示地图时,将显示Toast 。)

Any ideas?

(有任何想法吗?)

Well start the task

(好好开始任务)

EDIT:

(编辑:)

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

and in onPostExecute make a toast

(并在onPostExecute敬酒)

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

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

(在新的TestTask(this) ,这是对MapOverlay的引用,而不是对MapActivity的引用,因此这就是问题所在。)

  ask by Upvote translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(为了在您的应用程序中显示Toast ,请尝试以下操作:)

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

Another example:

(另一个例子:)

Toast.makeText(getActivity(), "This is my Toast message!",
   Toast.LENGTH_LONG).show();

We can define two constants for duration:

(我们可以为持续时间定义两个常量:)

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

(int LENGTH_LONG长时间显示视图或文本通知。)

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

(int LENGTH_SHORT短时间内显示视图或文本通知。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...