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的使用以及适配器的使用,有关它们的笔记我将会在后面补上。


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