編輯:Android開發實例
今天學習Android通知 Toast的用法,Toast在手機屏幕上向用戶顯示一條信息,一段時間後信息會自動消失。信息可以是簡單的文本,也可以是復雜的圖片及其他內容(顯示一個view)。
看效果圖:
今天演示的有兩種用法,如上圖
main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?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顯示View"
/>
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Toast直接輸出"
/>
</LinearLayout>
兩個按鈕,很簡單
程序代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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,"直接輸出測試", Toast.LENGTH_LONG).show();
}
};
public void showToast(){
LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=li.inflate(R.layout.toast,null);
//把布局文件toast.xml轉換成一個view
Toast toast=new Toast(this);
toast.setView(view);
//載入view,即顯示toast.xml的內容
TextView tv=(TextView)view.findViewById(R.id.tv1);
tv.setText("Toast顯示View內容");
//修改TextView裡的內容
toast.setDuration(Toast.LENGTH_SHORT);
//設置顯示時間,長時間Toast.LENGTH_LONG,短時間為Toast.LENGTH_SHORT,不可以自己編輯
toast.show();
}
}
下面是toast.xml的內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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>
源代碼下載:Toast演示 (21)
轉自:
Android JSON解析器
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android本地化
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
Android JSON解析器
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android圖片處理:識別圖像方向並顯示實例教程
在Android中使用ImageView顯示圖片的時候發現圖片顯示不正,方向偏了或者倒過來了。 解決這個問題很自然想到的分兩步走: 1、自動識別圖像方向,計算旋轉