android中toast提示是什么?

toast提示是Android系统中一种消息框类型,一种简易的消息提示框;是Android中用来显示提示信息的一种机制。

当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。用户将可能是在中间键入别的东西。

Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失。

下面用一个实例来看看如何使用Toast:

默认样式:Toast.makeText(getApplicationContext(), "默认Toast样式",

Toast.LENGTH_SHORT).show();

自定义显示位置:toast = Toast.makeText(getApplicationContext(),

"自定义位置Toast", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

带图片效果:toast = Toast.makeText(getApplicationContext(),

"带图片的Toast", Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(getApplicationContext());

imageCodeProject.setImageResource(R.drawable.icon);

toastView.addView(imageCodeProject, 0);

toast.show();

完全自定义:LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom,

(ViewGroup) findViewById(R.id.llToast));

ImageView image = (ImageView) layout

.findViewById(R.id.tvImageToast);

image.setImageResource(R.drawable.icon);

TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);

title.setText("Attention");

TextView text = (TextView) layout.findViewById(R.id.tvTextToast);

text.setText("完全自定义Toast");

toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

更多web开发知识,请查阅 HTML中文网 !!

以上就是android中toast提示是什么?的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他答疑