Original URL: http://www.pocketdigi.com/20100904/87.html
Learn about the use of Android notification toast today, toast displays a message to the user on the phone screen, and the information will automatically disappear after a period of time. The information can be simple text or complex pictures and other content (display a view).
See:
There are two uses for today's demo, such as
Main.xml:
- <? XML version="1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- >
- <button Android:id="@+id/button1"
- Android:layout_width="Fill_parent"
- Android:layout_height="Wrap_content"
- Android:text="toast Show view"
- />
- <button Android:id="@+id/button2"
- Android:layout_width="Fill_parent"
- Android:layout_height="Wrap_content"
- Android:text="Toast Direct output"
- />
- </LinearLayout>
Two buttons, very simple
Program code:
- Package com. Pocketdgig. Toast;
- Import android. App. Activity;
- Import android. Content. Context;
- Import android. Os. Bundle;
- Import android. View. Layoutinflater;
- Import android. View. View;
- Import android. View. View. Onclicklistener;
- Import android. Widget. Button;
- Import android. Widget. TextView;
- Import android. Widget. Toast;
- Public Class main extends Activity {
- /** Called when the activity is first created. * /
- @Override
- public void onCreate(Bundle savedinstancestate) {
- Super. OnCreate(savedinstancestate);
- Setcontentview(R. Layout. Main);
- button button1= (button)Findviewbyid(R. ID. Button1);
- Button1. Setonclicklistener(bt1lis);
- button button2= (button)Findviewbyid(R. ID. Button2);
- Button2. Setonclicklistener(bt2lis);
- }
- onclicklistener Bt1lis=new Onclicklistener() {
- @Override
- Public void onClick(View v) {
- Showtoast();
- }
- };
- onclicklistener Bt2lis=new Onclicklistener() {
- @Override
- Public void onClick(View v) {
- Toast. Maketext(main. This,"Direct output test", Toast. Length_long). show();
- }
- };
- public void showtoast() {
- Layoutinflater li= (layoutinflater)getsystemservice(Context. Layout_inflater_service);
- View View=li. Inflate(R. Layout. Toast,null);
- //Convert layout file Toast.xml to a view
- Toast Toast=new toast(this);
- Toast. Setview(view);
- //Load view, which displays the contents of Toast.xml
- TextView TV= (TextView)view. Findviewbyid(R. ID. TV1);
- TV. SetText("Toast display view content");
- //modify content in TextView
- Toast. Setduration(Toast. Length_short);
- //Set display time, long time Toast.length_long, short time for Toast.length_short, can not edit yourself
- Toast. Show();
- }
- }
Here's what's toast.xml:
- <? XML version="1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- >
- <imageview android:src="@drawable/toast"
- Android:layout_width="Wrap_content"
- Android:layout_height="Wrap_content"
- />
- <textview Android:id="@+id/tv1"
- Android:text=""
- Android:layout_width="Wrap_content"
- Android:layout_height="Wrap_content"
- />
- </LinearLayout>
Source code Download: [Download id= "4″]
©, frozen fish. Please respect the author's labor results, copy reproduced and retain the site link! Application Development Notes
"Go" notification toast verbose usage (show view)