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的几个常见用法,开发者可以根据自己的需求选择适合的用法来显示消息提示。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/851560.html