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

其他回答

内部片段(onCreateView)

Toast.makeText(getActivity(), "your message", Toast.LENGTH_LONG).show();

类内部(onCreate)

Toast.makeText (myClassName。this, "your message", Toast.LENGTH_LONG).show();

简单的方法

吐司(“你信息”)

OR

面包(R.string.some_message)

只需在BaseActivity中添加两个方法。如果您还没有使用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扩展所有的活动。

public class MainActivity extends BaseActivity

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

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

语法

Toast.makeText(context, text, duration);

参数值

上下文

getApplicationContext() -返回应用程序中运行的所有活动的上下文。 getBaseContext() -如果你想从应用程序中的另一个上下文访问Context,你可以访问。 getContext() -仅返回当前正在运行的活动的上下文视图。

text

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

 (string)num   // type caste

持续时间

吐司。LENGTH_SHORT -吐司延迟2000毫秒预定义的 吐司。LENGTH_LONG -预定义的烤面包延迟3500毫秒 毫秒-吐司延迟用户定义的毫秒(例如。4000)


Example.1

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

Example.2

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

如果你想在你的活动中写一段简单的祝酒词: Toast.makeText (getApplicationContext(),“你好”,Toast.LENGTH_SHORT),告诉();

1.在Toast中显示TextView:——

TextView tv =新的TextView(这); tv.setText(“你好!”); tv.setTextSize (30); tv.setTextColor (Color.RED); tv.setBackgroundColor (Color.YELLOW);

2.显示图像为吐司:-

ImageView iv =新的ImageView(this); iv.setImageResource (R.drawable.blonde); 吐司=新的吐司(这个); t.setView (iv); t.setDuration (Toast.LENGTH_LONG); t.show ();

3.显示布局为吐司:——

LayoutInflater li = getLayoutInflater(); View View = li. expand (r.b ayout.my_toast_layout,null,false); 吐司=新的吐司(这个); t.setView(查看); t.setDuration (Toast.LENGTH_LONG); t.show ();

**如果你想在Async中写吐司,那么: 私人活动活动; private android.content.Context 这一点。活动=活动; 这一点。上下文=上下文; 吐司。makeText(context, "Hello", Toast.LENGTH_SHORT).show();