Android Toast introduction-android learning journey (), toast-android
Toast Introduction
Toast is a very convenient message prompt box that displays a short message prompt on the desktop. There are two features:
1. The message does not obtain the focus.
2. It will automatically disappear in a period of time.
Toast generation steps
1. Call the constructor or the static method makeText () to generate a Toast.
2. Call Toast to set the method and margin of the message.
3. Call the show () method of Toast to display it.
Most Toast messages can only be displayed. If you want to display images and charts, you must use setView () to customize them.
Demo of message prompts with images
Package peng. liu. test; import android. app. activity; import android. graphics. color; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. baseAdapter; import android. widget. button; import android. widget. frameLayout; import android. widget. gridView; import android. widget. imageSwitcher; import android. widget. imageView; import android. widget. linearLayout; import android. widget. simpleAdapter; import android. widget. textSwitcher; import android. widget. textView; import android. widget. toast; import android. widget. viewFlipper; import android. widget. viewSwitcher; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; public class MainActivity extends Activity {private Button simple, complex; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); simple = (Button) findViewById (R. id. simple); complex = (Button) findViewById (R. id. complex); simple. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {Toast. makeText (MainActivity. this, "simple message prompt", Toast. LENGTH_LONG ). show () ;}}); complex. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {Toast comToast = new Toast (MainActivity. this); comToast. setGravity (Gravity. CENTER, 0, 0); comToast. setDuration (Toast. LENGTH_LONG); LinearLayout ll = new LinearLayout (MainActivity. this); ImageView image = new ImageView (MainActivity. this); image. setImageResource (R. drawable. ic_launcher); TextView text = new TextView (MainActivity. this); text. setText ("toast with image"); text. setTextSize (30); text. setTextColor (Color. MAGENTA); ll. addView (image); ll. addView (text); comToast. setView (ll); comToast. show ();}});}}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/simple" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/complex" android:layout_below="@+id/simple" android:layout_alignRight="@+id/simple" android:layout_alignEnd="@+id/simple" /></RelativeLayout>