Android開發祕籍學習筆記(四)

  關於Toast和AlertDialog的學習

Toast的使用:

  Toast警報僅用一行代碼就可以在屏幕上產生一條輸出消息,而且不需要修改佈局文件。因此,它對於調試工作也算是一種便利的工具,與C語言程序中的printf語句等價。
  在Android中使用Toast的方法的一種簡單形式:
Toast toast=Toast.makeText(getApplicationContext(), "默認的Toast", Toast.LENGTH_SHORT).show();  </span>
  makeText(Context , Strinng , long);
1 // 第一個參數:當前的上下文環境。可用getApplicationContext()或this 
2 // 第二個參數:要顯示的字符串。也可是R.string中字符串ID 
3 // 第三個參數:顯示的時間長短。Toast默認的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms 
  當需要改變文本位置的時候,可以在調用show()方法之前使用setGravity方法:
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對話框的使用:

  我們在使用別人的APP時經常會用到對話框AlertDialog,AlertDialog和Toast一樣也可以用來提供消息傳送,但其功能要比Toast強不少。Android中通過Dialog Builder該接口讓我們可以自定義對話框。
  首先是最簡單的AlertDialog:
  
new AlertDialog.Builder(MainActivity.this)
 .setTitle("標題") 
 .setMessage("簡單消息框")
 	.setPositiveButton("確定", null)
 	.show();

上面的代碼中,我們新建了一個AlertDialog,並用Builder方法形成了一個對象鏈,通過一系列的設置方法,構造出了我們需要的對話框,然後調用show方法顯示出來,注意到Builder方法中的參數是“上下文”---Context,這裏可以使用......Activity.this。

下面是帶確認和取消按鈕的對話框:
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();


  其實在AlertDialog還能加入多選框,列表框,多選單選框等,這裏就不一一列舉了。具體的代碼見

  AlertDialog還不止上述的功能,還能做成彈窗。像現在基本所有手機都支持文件夾形式了(即把APP應用程序放到一個文件夾裏),而打開文件夾就是一個彈窗。先上效果圖:

該應用的佈局文件很簡單,就是一個Button即可;
<?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用來顯示應用的名稱。
  其實AlertDialog的彈窗原理和AlertDialog中加入多選框是一樣的,都是setView()了一個新的視圖,都算自定義的AlertDialog,不同的只是你setView()的視圖是不是需要AdapterView視圖(適配器)。

  不過,上面的代碼中有兩個常用的知識點HashMap的使用以及適配器的使用,有關它們的筆記我將會在後面補上。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章