如何在Android中显示Toast?

问题描述 投票:418回答:19

我有一个可以拉起来的滑块,然后它会显示一张地图。我可以上下移动滑块来隐藏或显示地图。当地图在前面时,我可以处理该地图上的触摸事件。每当我触摸时,AsyncTask都会启动,它会下载一些数据并生成一个显示数据的Toast。虽然我在触摸事件上启动任务但是没有显示吐司,直到我关闭滑块。当滑块关闭并且不再显示地图时,会出现Toast

有任何想法吗?

好了开始任务

编辑:

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

并在onPostExecute祝酒

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

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

android android-mapview android-asynctask toast
19个回答
828
投票

要在您的应用程序中显示Toast,请尝试以下方法:

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

另一个例子:

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

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

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

7
投票
Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                   Toast.LENGTH_LONG).show();

而不是使用“app.getBaseContext()”。

您可以尝试使用“getApplicationContext()”或“getContext()”。

如果您的代码处于活动状态,那么您应该使用“Activity.this”的“this”。 如果您的代码是片段,那么你应该去“getActivity()”


6
投票

要显示Toast,请使用以下代码:

Toast.makeText(app.getBaseContext(),"your string",Toast.LENGTH_SHORT).show();

Toast toast = new Toast(getApplicationContext());

5
投票
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

toast.show();

4
投票

Toast toast=Toast.makeText(getApplicationContext(),"Hello", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); // last two args are X and Y are used for setting position toast.setDuration(10000);//you can even use milliseconds to display toast toast.show();**//showing the toast is important**

enter image description here

句法

Must read: Android Toast Example

您可以使用getApplicationContext()或getActivity()或MainActivity.this(如果Activity Name是MainActivity)

Toast.makeText(context, text, duration);

要么

Toast.makeText(getApplicationContext(),"Hi I am toast",Toast.LENGTH_LONG).show();

3
投票

Simple Way

Toast.makeText(MainActivity.this,"Hi I am Toast", Toast.LENGTH_LONG).show();

要么

toast("Your Message")

只需在toast(R.string.some_message)中添加两个方法即可。如果您还没有使用,请创建新的BaseActivity

BaseActivity

并通过public class BaseActivity extends AppCompatActivity { public void toast(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } public void toast(@StringRes int msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } } 扩展您的所有活动。

BaseActivity

2
投票

最简单的方法! (要在主要活动中显示,请替换其他活动的第一个参数)

public class MainActivity extends BaseActivity

1
投票

这对我有用:

Button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Toast.makeText(MainActivity.this,"Toast Message",Toast.LENGTH_SHORT).show();
    }
}

0
投票

Show Toast from Service

Toast.makeText(getBaseContext(), "your text here" , Toast.LENGTH_SHORT ).show();

您还可以将public class ServiceA extends Service { //.... public void showToast(final String message) { Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show(); } }); } //.... } 方法放在Application类中,并从任何地方显示toast。


0
投票

如果你想在你的活动中写一个简单的吐司:showToast

1.在Toast中显示TextView:---

Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show(); TextView tv = new TextView(this); tv.setText("Hello!"); tv.setTextSize(30); tv.setTextColor(Color.RED);

2.将图像显示为吐司: -

tv.setBackgroundColor(Color.YELLOW); ImageView iv = new ImageView(this); iv.setImageResource(R.drawable.blonde); Toast t = new Toast(this); t.setView(iv); t.setDuration(Toast.LENGTH_LONG);

3.将布局显示为Toast: -

t.show(); LayoutInflater li = getLayoutInflater(); View view = li.inflate(R.layout.my_toast_layout,null,false); Toast t = new Toast(this); t.setView(view); t.setDuration(Toast.LENGTH_LONG);

**如果你想在你的异步中写吐司那么:t.show(); private Activity activity; private android.content.Context context; this.activity = activity; this.context = context;


0
投票

这是另一个:

Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();

refreshBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getBaseContext(),getText(R.string.refresh_btn_pushed),Toast.LENGTH_LONG).show(); } }); 在哪里:

Toast

&Qazxswpoi:

Toast.makeText(getBaseContext(),getText(R.string.refresh_btn_pushed),Toast.LENGTH_LONG).show();


77
投票

使用baseadapter扩展活动使用了这个

Toast.makeText(getActivity(), 
    "Your Message", Toast.LENGTH_LONG).show();

或者如果您正在使用活动或mainactivity

Toast.makeText(MainActivity.this, 
    "Your Message", Toast.LENGTH_LONG).show();

42
投票

Syntax

Toast.makeText(context, text, duration);

Parameter Value

context

getApplicationContext() - 返回在应用程序中运行的所有活动的上下文。

getBaseContext() - 如果要从应用程序中的其他上下文访问Context,则可以访问。

getContext() - 仅返回当前运行活动的上下文视图。

text

text - 返回“STRING”,如果不是字符串,则可以使用类型转换。

 (string)num   // type caste

duration

Toast.LENGTH_SHORT - 吐司延迟2000毫秒预定义

Toast.LENGTH_LONG - 预定义的吐司延迟3500毫秒

qazxsw poi - Toast延迟用户定义的毫秒数(例如4000)


Example.1

milisecond

Example.2

Toast.makeText(getApplicationContext(), "STRING MESSAGE", Toast.LENGTH_LONG).show();

21
投票

在Android中祝酒

Toast.makeText(getApplicationContext(), "STRING MESSAGE", 5000).show();

要么

Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_SHORT).show();

(LENGTH_SHORT和LENGTH_LONG充当布尔标志 - 这意味着您无法将吐司计时器发送到毫秒,但您需要使用这两个选项中的任何一个)


20
投票

你可以自定义你的东西:

Toast.makeText(MainActivity.this, "YOUR MESSAGE", LENGTH_LONG).show();

或一般方式:

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

15
投票

我尝试了几次吐司,并为那些他们的吐司给他们错误的人尝试

Toast.makeText(context,"Your message.", Toast.LENGTH_LONG).show();

14
投票

有两种方法可以做到这一点。

使用内置的Toast消息

Toast.makeText(getApplicationContext(), "google", Toast.LENGTH_LONG).show();

或通过提供自定义布局文件来制作自定义文件

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

9
投票

我在这里碰到了答案,并被似乎有人捅人的事实所吸引,他们认为需要一个活动背景。不是这种情况。但是,要求从主事件或UI线程发布Toast。因此,让它在活动上下文之外工作有点棘手。这是一个可以在系统服务内部工作的示例,或者最终从Toast myToast = new Toast(getApplicationContext()); myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); myToast.setDuration(Toast.LENGTH_LONG); myToast.setView(myLayout); myToast.show(); 继承的任何潜在类。

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(); } }); } } 的实例来实现此功能。请停止提示是这种情况!如果需要Activity,方法签名将不会要求Activity


7
投票

如果它是碎片,

Context
© www.soinside.com 2019 - 2024. All rights reserved.