我有一个滑块,可以拉上来,然后它会显示一个地图。我可以上下移动滑块来隐藏或显示地图。当地图在前面时,我可以处理地图上的触摸事件。每次我触摸,一个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();

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


当前回答

 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**

其他回答

你可以自定义你的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();

或General way:

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

为了在安卓系统中举杯,

import android.widget.Toast;


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

or

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

(LENGTH_SHORT和LENGTH_LONG作为布尔标志-这意味着你不能发送吐司计时器毫秒,但你需要使用这两个选项中的任何一个)

这招对我很管用:

Toast.makeText(getBaseContext(), "your text here" , 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.makeText (getBaseContext (), getText (R.string.refresh_btn_pushed) Toast.LENGTH_LONG),告诉();

& strings.xml:

<string name=" refresh_btn_pushing ">"Refresh was Clicked…"字符串> < /

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

而不是使用"app.getBaseContext()"。

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

如果你的代码处于活动状态,那么你应该使用" activity - this"中的"this"。 如果你的代码是分段的,那么你应该使用getActivity()