Basic tutorial on Android -- 2.5.1 basic use of Toast (Toast)

Source: Internet
Author: User

Basic tutorial on Android -- 2.5.1 basic use of Toast (Toast)
 

This section introduces:

Okay, I finally learned some controls related to the Adapter class. Of course, there are many other
The related controls will not be explained slowly ~ If you need to read the documentation and view related usage, this section provides the following information:
Android is a control used to prompt information-Toast (Toast )! Toast is a convenient message prompt box
A message prompt box is displayed on the screen. Without any buttons, the focus will not automatically disappear after a period of time!
Very common! This section describes how to use Toast!

1. directly call the makeText () method of the Toast class to create

This is the most widely used form! For example, you can click a button and then pop up Toast. Usage:
Toast. makeText (MainActivity. this, "Content prompted", Toast. LENGTH_LONG). show ();
The first is the context object! The two items are displayed! The third is the display time, which can only be LONG or SHORT.
It will take effect. You have defined other values, and the last two values are called!

In addition, Toast is very common. We can extract these public parts and write them into a method!
You can call this method to display the Toast when you need to display the Toast, which is much more convenient!
Example:

Void midToast (String str, int showTime) {Toast toast = Toast. makeText (global_context, str, showTime); toast. setGravity (Gravity. CENTER_VERTICAL | Gravity. CENTER_HORIZONTAL, 0, 0); // sets the display position TextView v = (TextView) toast. getView (). findViewById (android. r. id. message); v. setTextColor (Color. YELLOW); // set the font color toast. show ();}

The method extracted above shows that we can call setGravity to set the Toast display position and obtain
Use findViewById (android. R. id. message) to obtain the displayed text, and then set the color or size!
This is the second method to customize Toast through the constructor!

2. Use the constructor to customize Toast:

The text and display position have been customized above. Here are two simple examples:

1. Define a Toast with an image

:

Key code:

Private void midToast (String str, int showTime) {Toast toast = Toast. makeText (mContext, str, showTime); toast. setGravity (Gravity. CENTER_HORIZONTAL | Gravity. BOTTOM, 0, 0); // set the display position LinearLayout layout = (LinearLayout) toast. getView (); layout. setBackgroundColor (Color. BLUE); ImageView image = new ImageView (this); image. setImageResource (R. mipmap. ic_icon_qitao); layout. addView (image, 0); TextView v = (TextView) toast. getView (). findViewById (android. r. id. message); v. setTextColor (Color. YELLOW); // set the font color toast. show ();}
2. Full Toast Customization

If the above content does not meet your needs, you can write a Toast layout and display it on your own;
But we still cannot control the time!

Run:

Key code:

    private void midToast(String str, int showTime)    {        LayoutInflater inflater = getLayoutInflater();        View view = inflater.inflate(R.layout.view_toast_custom,                (ViewGroup) findViewById(R.id.lly_toast));        ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);        TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);        tv_msg.setText(str);        Toast toast = new Toast(mContext);        toast.setGravity(Gravity.CENTER, 0, 0);        toast.setDuration(Toast.LENGTH_LONG);        toast.setView(view);        toast.show();    }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.