Android消息提示框Toast
Toast是Android中一種簡易的消息提示框。和Dialog不一樣的是,Toast是沒有焦點的,toast提示框不能被用戶點擊,而且Toast顯示的時間有限,toast會根據用戶設置的顯示時間後自動消失。
創建Toast的方法總共有2種:
1.Toast.makeText(Context context, (CharSequence text)/( int
resId), int duration)
參數:context是指上下文對象,通常是當前的Activity,text是指自己寫的消息內容,resId指顯示內容引用Resource的那條數據,duration指Toast的顯示時間,Toast默認有LENGTH_SHORT和LENGTH_LONG兩常量,分別表示時間長和短,也可以自定義時間,如:5000表示顯示5秒。用此方法獲得Toast對象之後調用.show()方法即可顯示消息。
2.自定義Toast,通過Toast
toast = new Toast(Context context)獲得Toast對象,調用.show()方法即可顯示消息。
toast.setDuration(duration)設置顯示時間,
toast.setGravity(gravity,
xOffset, yOffset)設置顯示位置,三個參數分別表示(起點位置,水平位移,垂直位移),
toast.setMargin(float
horizontalMargin, float verticalMargin)以橫向和縱向的百分比設置顯示位置,參數均爲float類型(水平位移正右負左,豎直位移正上負下),
toast.setText(text)設置消息內容,
toast.setView(view)設置Toast顯示的視圖組件。
實例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<Button
android:id="@+id/simpleToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="默認" >
</Button>
<Button
android:id="@+id/simpleToastWithCustomPosition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定義顯示位置" >
</Button>
<Button
android:id="@+id/imageToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="帶圖片" >
</Button>
<Button
android:id="@+id/customToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="完全自定義" >
</Button>
<Button
android:id="@+id/runToastFromOtherThread"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="其他線程" >
</Button>
</LinearLayout>
custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toastLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#333399"
android:gravity="center"
android:text="ToastTitle"
android:textColor="#ffffff" />
<LinearLayout
android:id="@+id/toastContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="#3366ff"
android:orientation="vertical"
android:padding="15dp" >
<ImageView
android:id="@+id/customImageToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/customTextToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private Button simpleToastBtn, simpleToastWithCustomPositionBtn,
imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleToastBtn = (Button) findViewById(R.id.simpleToast);
simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);
imageToastBtn = (Button) findViewById(R.id.imageToast);
customToastBtn = (Button) findViewById(R.id.customToast);
runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);
simpleToastBtn.setOnClickListener(this);
simpleToastWithCustomPositionBtn.setOnClickListener(this);
imageToastBtn.setOnClickListener(this);
customToastBtn.setOnClickListener(this);
runToastFromOtherThreadBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast toast = null;
switch (v.getId()) {
case R.id.simpleToast:
Toast.makeText(MainActivity.this, "默認Toast樣式", Toast.LENGTH_LONG)
.show();
break;
case R.id.simpleToastWithCustomPosition:
toast = Toast.makeText(MainActivity.this, "自定義位置Toast",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case R.id.imageToast:
toast = Toast.makeText(MainActivity.this, "帶圖片的Toast",
Toast.LENGTH_LONG);
LinearLayout layout = (LinearLayout) toast.getView();
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
layout.addView(image, 0);
toast.show();
break;
case R.id.customToast:
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.custom, null);
ImageView customImage = (ImageView) view
.findViewById(R.id.customImageToast);
TextView customText = (TextView) view
.findViewById(R.id.customTextToast);
customImage.setImageResource(R.drawable.ic_launcher);
customText.setText("完全自定義Toast");
toast = new Toast(MainActivity.this);
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
break;
case R.id.runToastFromOtherThread:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
break;
}
}
public void showToast() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "我來自其他線程", Toast.LENGTH_LONG)
.show();
}
});
}
}