如何在 Android 中显示 Toast?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3500197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display Toast in Android?
提问by Upvote
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.
我有一个可以拉起的滑块,然后它会显示一张地图。我可以上下移动滑块来隐藏或显示地图。当地图在前面时,我可以处理该地图上的触摸事件。每次我触摸时,aAsyncTask
都会被启动,它会下载一些数据并制作一个Toast
显示数据的 a。虽然我在触摸事件上开始任务,但没有显示吐司,直到我关闭滑块。当滑块关闭且地图不再显示时,会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.
在 new 中TestTask(this)
,这是对MapOverlay
而不是对的引用MapActivity
,所以这就是问题所在。
回答by Jorgesys
In order to display Toastin 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_LONGShow the view or text notification for a long period of time.
int LENGTH_SHORTShow the view or text notification for a short period of time.
int LENGTH_LONG 长时间显示视图或文本通知。
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();
回答by Cristiana Chavez
Extending activity using baseadapter
used this
使用baseadapter
used this扩展活动
Toast.makeText(getActivity(),
"Your Message", Toast.LENGTH_LONG).show();
or if you are using activity or mainactivity
或者如果您正在使用活动或 mainactivity
Toast.makeText(MainActivity.this,
"Your Message", Toast.LENGTH_LONG).show();
回答by Jay Patel
Syntax
句法
Toast.makeText(context, text, duration);
Parameter Value
参数值
context
语境
getApplicationContext()
- Returns the context for all activities running in application.
getBaseContext()
- If you want to access Context from another context within application you can access.
getContext()
- Returns the context view only current running activity.
getApplicationContext()
- 返回在应用程序中运行的所有活动的上下文。
getBaseContext()
- 如果您想从可以访问的应用程序内的另一个上下文访问上下文。
getContext()
- 仅返回当前正在运行的活动的上下文视图。
text
文本
text
- Return "STRING" , If not string you can use type cast.
text
- 返回 "STRING" ,如果不是字符串,您可以使用类型转换。
(string)num // type caste
duration
期间
Toast.LENGTH_SHORT
- Toast delay 2000 ms predefined
Toast.LENGTH_LONG
- Toast delay 3500 ms predefined
milisecond
- Toast delay user defined miliseconds (eg. 4000)
Toast.LENGTH_SHORT
- Toast 延迟 2000 ms 预定义
Toast.LENGTH_LONG
- Toast 延迟 3500 ms 预定义
milisecond
- Toast 延迟用户定义的毫秒(例如 4000)
Example.1
示例.1
Toast.makeText(getApplicationContext(), "STRING MESSAGE", Toast.LENGTH_LONG).show();
Example.2
例 2
Toast.makeText(getApplicationContext(), "STRING MESSAGE", 5000).show();
回答by Dinesh
To toast in Android
在 Android 中敬酒
Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_SHORT).show();
or
或者
Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_LONG).show();
( LENGTH_SHORT and LENGTH_LONG are acting as boolean flags - which means you cant sent toast timer to miliseconds, but you need to use either of those 2 options )
( LENGTH_SHORT 和 LENGTH_LONG 充当布尔标志 - 这意味着您不能将吐司计时器发送到毫秒,但您需要使用这两个选项中的任何一个)
回答by Mahesh
You can customize your tost:
您可以自定义您的 tost:
LayoutInflater mInflater=LayoutInflater.from(this);
View view=mInflater.inflate(R.layout.your_layout_file,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
Or General way:
或一般方式:
Toast.makeText(context,"Your message.", Toast.LENGTH_LONG).show();
回答by Mahesh
I have tried several toast and for those whom their toast is giving them error try
我已经尝试了几种吐司,对于那些吐司给他们错误的人来说尝试
Toast.makeText(getApplicationContext(), "google", Toast.LENGTH_LONG).show();
回答by Aniket Thakur
There are two ways to do it.
有两种方法可以做到。
Either use the inbuilt Toast message
要么使用内置的 Toast 消息
//Toast shown for short period of time
Toast.makeText(getApplicationContext(), "Toast Message", Toast.LENGTH_SHORT).show();
//Toast shown for long period of time
Toast.makeText(getApplicationContext(), "Toast Message", Toast.LENGTH_LONG).show();
or make a custom one by providing custom layout file
或通过提供自定义布局文件来定制一个
Toast myToast = new Toast(getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.setView(myLayout);
myToast.show();
回答by ChrisCM
I ran across the answers here, and was attracted by the fact that there seems to be someone poking around, believing that an Activity context is required. This is not the case. However, it is a requirement that a Toast is posted from the main event or UI Thread. So, getting this to work outside of an activity context is a little bit tricky. Here is an example that would work inside of a system service, or any potential class that ultimately inherits from Context
.
我在这里找到了答案,并被这样一个事实所吸引:似乎有人四处闲逛,认为需要 Activity 上下文。不是这种情况。但是,要求从主事件或 UI 线程发布 Toast。所以,让它在活动上下文之外工作有点棘手。这是一个可以在系统服务或最终从Context
.
public class MyService extends AccessibilityService {
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
Note that we do not need access to an instance of Activity
for this to work. Please stop suggesting this is the case! If Activity
were required, the method signature wouldn't call for a Context
.
请注意,我们不需要访问 的实例Activity
即可使其工作。请停止暗示是这种情况!如果Activity
需要,方法签名不会调用Context
.
回答by Yogesh Nandha
Toast.makeText(app.getBaseContext(),"your string",Toast.LENGTH_SHORT).show();
instead of using "app.getBaseContext()".
而不是使用“app.getBaseContext()”。
You can try using "getApplicationContext()" or "getContext()".
您可以尝试使用“ getApplicationContext()”或“ getContext()”。
If your code is in activity then you should use "this" of "Activty.this".
If your code is in fragment then you should go for "getActivity()"
如果您的代码处于活动状态,那么您应该使用“Activty.this”中的“this”。
如果您的代码在片段中,那么您应该选择“getActivity()”
回答by hash
If it's fragment,
如果是碎片,
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();