Toast prompt is a type of message box in the Android system, a simple message prompt box, and a mechanism used to display prompt information in Android; the idea of the Toast class is to be as unobtrusive as possible , while also displaying information to the user.
#The operating environment of this article: Windows 7 system, Dell G3 computer.
Toast prompt in android
Toast prompt is a type of message box in the Android system, a simple message prompt box; it is used to display prompt information in Android a mechanism.
When the view is displayed to the user, it appears as a float in the application. Unlike Dialog, it never gets focus and cannot be clicked. The user will probably type something else in the middle.
The idea of the Toast class is to be as unobtrusive as possible while still displaying information to the user in the hope that they will see it. Moreover, the display time of Toast is limited, and Toast will automatically disappear after the display time set by the user.
Let’s use an example to see how to use 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();
For more related knowledge, please visit PHP Chinese website!
The above is the detailed content of What is a toast prompt?. For more information, please follow other related articles on the PHP Chinese website!