關於Toast和AlertDialog的學習
Toast的使用:
Toast toast=Toast.makeText(getApplicationContext(), "默認的Toast", Toast.LENGTH_SHORT).show(); </span>
makeText(Context , Strinng , long);2 // 第二個參數:要顯示的字符串。也可是R.string中字符串ID
3 // 第三個參數:顯示的時間長短。Toast默認的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms
Toast toast=Toast.makeText(getApplicationContext(), "自定義顯示位置的Toast", Toast.LENGTH_SHORT);
//第一個參數:設置toast在屏幕中顯示的位置。我現在的設置是居中靠頂
//第二個參數:相對於第一個參數設置toast位置的橫向X軸的偏移量,正數向右偏移,負數向左偏移
//第三個參數:同的第二個參數道理一樣
//如果你設置的偏移量超過了屏幕的範圍,toast將在屏幕內靠近超出的那個邊界顯示
toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);
//屏幕居中顯示,X軸和Y軸偏移量都是0
//toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();</span>
要給Toast添加圖像,可使用下面的代碼:Toast toast=Toast.makeText(getApplicationContext(), "顯示帶圖片的toast", 3000);
toast.setGravity(Gravity.CENTER, 0, 0);
//創建圖片視圖對象
ImageView imageView= new ImageView(getApplicationContext());
//設置圖片
imageView.setImageResource(R.drawable.ic_launcher);
//獲得toast的佈局
LinearLayout toastView = (LinearLayout) toast.getView();
//設置此佈局爲橫向的
toastView.setOrientation(LinearLayout.HORIZONTAL);
//將ImageView在加入到此佈局中的第一個位置
toastView.addView(imageView, 0);
toast.show();
後來發現網上還有一種自定義的Toast形式,這裏借鑑下://Inflater意思是充氣
//LayoutInflater這個類用來實例化XML文件到其相應的視圖對象的佈局
LayoutInflater inflater = getLayoutInflater();
//通過制定XML文件及佈局ID來填充一個視圖對象
View layout = inflater.inflate(R.layout.custom2,(ViewGroup)findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
//設置佈局中圖片視圖中圖片
image.setImageResource(R.drawable.ic_launcher);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
//設置標題
title.setText("標題欄");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
//設置內容
text.setText("完全自定義Toast");
Toast toast= new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER , 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
不過感覺自定義的Toast有些沒必要的,論美觀程度它也無法和自定義的AlertDialog比較,更何況Toast只是用來顯示一條信息的沒有必要做的如此花哨。AlertDialog對話框的使用:
new AlertDialog.Builder(MainActivity.this)
.setTitle("標題")
.setMessage("簡單消息框")
.setPositiveButton("確定", null)
.show();
下面是帶確認和取消按鈕的對話框:
new AlertDialog.Builder(self)
.setTitle("消息框的標題")
.setMessage("確定嗎?")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.show();
在setPositiveButton中的第二個參數是設置按鈕的點擊監聽事件,設置null即不需要監聽該動作new AlertDialog.Builder(self)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(new EditText(self))
.setPositiveButton("確定", null)
.setNegativeButton("取消", null)
.show();
<?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" android:background="@drawable/apple1600_3018" android:id="@+id/parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dialog"></Button>
</LinearLayout>
接下來就需要給Button(dialog)綁定監聽事件了----直接調用一個opendialog()函數:private void openDialog() {
View menuView = View.inflate(this, R.layout.gridview_menu, null);
// 創建AlertDialog
final AlertDialog menuDialog = new AlertDialog.Builder(this).create();
menuDialog.setView(menuView);
menuGrid = (GridView) menuView.findViewById(R.id.gridview);
menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
menuGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 == 11) {
menuDialog.cancel();
}
}
});
menuDialog.show();
}
private ListAdapter getMenuAdapter(String[] menuNameArray,
int[] menuImageArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < menuNameArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemImage", menuImageArray[i]);
map.put("itemText", menuNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.item_menu, new String[] { "itemImage", "itemText" },
new int[] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
這裏又用到了另一個佈局文件---gridview_menu.xml,該佈局文件中就只需要定義一個Gridview的視圖就可以了,在適配時又用到了item_menu.xml,該佈局文件中就定義兩個控件----一個是ImageView用來顯示圖標,另一個是TextView用來顯示應用的名稱。