关于Toast和AlertDialog的学习
Toast toast=Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT).show(); </span>makeText(Context , Strinng , long);
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只是用来显示一条信息的没有必要做的如此花哨。
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用来显示应用的名称。