消息模式Toast.makeText的几种常见用法

   2024-11-05 6300
核心提示:Toast.makeText是Android中用于显示短暂的消息提示的工具类,常见的用法有以下几种:显示简单的文本消息:Toast.makeText(contex

Toast.makeText是Android中用于显示短暂的消息提示的工具类,常见的用法有以下几种:

显示简单的文本消息:

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

设置消息的显示时长:

Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();

自定义消息的位置:

Toast toast = Toast.makeText(context, "Hello World!", Toast.LENGTH_SHORT);toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);toast.show();

使用自定义的布局显示消息:

LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.custom_toast, findViewById(R.id.custom_toast_container));TextView text = layout.findViewById(R.id.text);text.setText("Hello World!");Toast toast = new Toast(getApplicationContext());toast.setDuration(Toast.LENGTH_SHORT);toast.setView(layout);toast.show();

在后台线程中使用Toast消息:

runOnUiThread(new Runnable() {    @Override    public void run() {        Toast.makeText(context, "Hello World!", Toast.LENGTH_SHORT).show();    }});

这些是Toast.makeText的几个常见用法,开发者可以根据自己的需求选择适合的用法来显示消息提示。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号