Android development step by step 32: Toast and androidtoast

Source: Internet
Author: User

Android development step by step 32: Toast and androidtoast

Toast has the English name Tusi. In Android, this class is used to pop up the prompt information. I think the sdk author thinks that the prompt message is like a piece of Toast. We will not talk about this theory much, so we will start to practice it.
Step 1: Design the page
Home Page toastview. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_height = "wrap_content" android: text = "default" android: id = "@ + id/btnSimpleToast" android: layout_width = "fill_parent" android: layout_alignParentTop = "true" android: layout_alignParentLeft = "true"> </Button>
<Button android: layout_height = "wrap_content" android: text = "Custom display location" android: id = "@ + id/btnSimpleToastWithCustomPosition" android: layout_width = "fill_parent" android: layout_below = "@ + id/btnSimpleToast" android: layout_alignParentLeft = "true" android: layout_marginTop = "21dp"> </Button>
<Button android: layout_height = "wrap_content" android: text = "with image" android: id = "@ + id/btnSimpleToastWithImage" android: layout_width = "fill_parent" android: layout_below = "@ + id/btnSimpleToastWithCustomPosition" android: layout_alignParentLeft = "true" android: layout_marginTop = "20dp"> </Button>
<Button android: layout_height = "wrap_content" android: text = "other threads" android: id = "@ + id/btnRunToastFromOtherThread" android: layout_width = "fill_parent" android: layout_below = "@ + id/btnCustomToast" android: layout_alignParentLeft = "true" android: layout_marginTop = "26dp"> </Button>
<Button android: layout_height = "wrap_content" android: text = "Custom" android: id = "@ + id/btnCustomToast" android: layout_width = "fill_parent" android: layout_below = "@ + id/btnSimpleToastWithImage" android: layout_alignParentLeft = "true" android: layout_marginTop = "19dp"> </Button>
</RelativeLayout>
 
Custom toast page mmtoast. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_height = "wrap_content" android: layout_width = "wrap_content"
Android: background = "# ffffffff" android: orientation = "vertical"
Android: id = "@ + id/llToast">
 
<TextView android: layout_height = "wrap_content"
Android: layout_margin = "1dip" android: textColor = "# ffffffff"
Android: layout_width = "fill_parent" android: gravity = "center"
Android: background = "# bb000000" android: id = "@ + id/tvTitleToast"/>
<LinearLayout android: layout_height = "wrap_content"
Android: orientation = "vertical" android: id = "@ + id/llToastContent"
Android: layout_marginLeft = "1dip" android: layout_marginRight = "1dip"
Android: layout_marginBottom = "1dip" android: layout_width = "wrap_content"
Android: padding = "15dip" android: background = "#44000000">
<ImageView android: layout_height = "wrap_content"
Android: layout_gravity = "center" android: layout_width = "wrap_content"
Android: id = "@ + id/imgToast"/>
<TextView android: layout_height = "wrap_content"
Android: paddingRight = "10dip" android: paddingLeft = "10dip"
Android: layout_width = "wrap_content" android: gravity = "center"
Android: textColor = "# ff000000" android: id = "@ + id/tvTextToast"/>
</LinearLayout>
</LinearLayout>
 
 
Step 2: Design actti1_toastactivity. java
 
/**
*
*/
Package com. figo. helloworld;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Import android. widget. Toast;
 
/**
* @ Author Administrator
*
*/
Public class ToastActivity extends Activity implementsOnClickListener {
Handler handler = new Handler (); // helps the main thread and subthread communicate
 


@ Override
Public void onCreate (BundlesavedInstanceState ){
Super. onCreate (savedInstanceState );
// Find the page
SetContentView (R. layout. toastview );
// Add button event
FindViewById (R. id. btnSimpleToast). setOnClickListener (this );
FindViewById (R. id. btnSimpleToastWithCustomPosition). setOnClickListener (
This );
FindViewById (R. id. btnSimpleToastWithImage). setOnClickListener (this );
FindViewById (R. id. btnCustomToast). setOnClickListener (this );
FindViewById (R. id. btnRunToastFromOtherThread). setOnClickListener (this );
 
}
 
Public void showToast (){
Handler. post (new Runnable (){
 
@ Override
Public void run (){
Toast. makeText (getApplicationContext (), "I'm from another thread! ",
Toast. LENGTH_SHORT). show ();
 
}
});
}
 
@ Override
Public void onClick (View v ){
Toast toast = null;
Switch (v. getId ()){
Case R. id. btnSimpleToast: // simple toast
Toast. makeText (getApplicationContext (), "Default Toast style ",
Toast. LENGTH_SHORT). show ();
Break;
CaseR. id. btnSimpleToastWithCustomPosition: // toast of the custom position
Toast = Toast. makeText (getApplicationContext (), "Custom location Toast ",
Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Break;
CaseR. id. btnSimpleToastWithImage: // toast with image
Toast = Toast. makeText (getApplicationContext (), "Toast with image ",
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 ();
Break;
Case R. id. btnCustomToast: // custom toast
LayoutInflaterinflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout
. FindViewById (R. id. imgToast );
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 ("Fully customized Toast ");
Toast = newToast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 100, 60 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();
Break;
CaseR. id. btnRunToastFromOtherThread: // after other threads are executed, toast is displayed in the main thread.
New Thread (new Runnable (){
Public void run (){
ShowToast ();
}
}). Start ();
Break;
 
}
 
}
 
}
 
Step 3: Register Activity with AndroidManifest. xml
<Activity android: name = ". ToastActivity" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
Step 4. Running Effect
 


Android development: Download is complete. No matter which page I am on, I want to have a Toast prompt indicating "download completed", but Toast cannot be global.

You can separate the main activity from other activities. You can inherit an activity class written by yourself, or you can use the message passing and message flag to add and judge. Thank you!

In android development, I wrote the following two lines so that toast does not repeat. Why not?

Your Toast is not an object. Is it estimated that you will only execute else content?
The Toast. makeText Example statement is not a method for calling the Toast object ~ Because you don't have a new Toast object, do you?

This call method is actually to call the static method of the Toast class (static Keyword Method), and no new object is created.

Your idea is not to repeat Toast, so you can define a Toast object globally, and then you can determine ~~
However, toast = null should not work .. The cancel method just disappears when toast appears. It does not empty the toast object, so let's modify it ~~ You can define a boolean to judge it ~~

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.