Android新手入门2016(12)--基于Layout文件的AlertDialog
来源:互联网 发布:推广淘宝店 编辑:程序博客网 时间:2024/06/06 12:55
本文来自肥宝传说之路,引用必须注明出处!
上一章学习了AlertDialog,后来发现还有基于Layout文件的AlertDialog。可以自己排好位置,相对复杂一点。
先看看效果
图中已经按布局文件排好位置了。
新加了个Layout文件:dialog_layout.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="wrap_content" android:orientation="vertical"> <EditText android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/edtInput"/> </LinearLayout>
这个布局文件只加入了一个输入框
然后看看代码:直接在上一章的基础上改的代码,好像有点多。
package com.fable.helloworld; import android.app.Activity; import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle; import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.EditText;import android.widget.GridView;import android.widget.SimpleAdapter; import java.util.*;public class HelloWorldActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_world); //设置主布局文件 GridView gridview = (GridView) findViewById(R.id.gridview); //创造数据来源 ArrayList<HashMap<String, Object>> images = new ArrayList<HashMap<String, Object>>(); for(int i=1;i<10;i++) { String imageName = ""; switch(i) { case 1: imageName = "AlertDialog";//普通的AlertDialog break; case 2: imageName = "AlertDialog2";//基于布局的AlertDialog break; default: imageName = "app"+String.valueOf(i); } HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.ic_launcher);//添加图像资源的ID,标识符,值 map.put("ItemText", imageName);//按序号做ItemText,标识符,值 images.add(map); } //把数据传入适配器,转换成布局需要的数据 SimpleAdapter simpleAdapter = new SimpleAdapter(this, //上下文为当前Activity images,//数据来源 R.layout.my_list_item,//每一项的布局的XML实现 new String[] {"ItemImage","ItemText"},//动态数组与ImageItem对应的子项 new int[] {R.id.ItemImage,R.id.ItemText}); //ImageItem的XML文件里面的一个ImageView,两个TextView ID //添加并且显示 gridview.setAdapter(simpleAdapter); //添加消息处理 gridview.setOnItemClickListener(new ItemClickListener()); } //当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件 class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0,//父视图 View arg1,//当前视图 int arg2,//点击的位置 long arg3//id ) { HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2); //获取点击的item //setTitle((String)item.get("ItemText")); //这个只是把标题改一改, String itemStr = (String)item.get("ItemText"); if(itemStr.equals("AlertDialog")){ showDialog(HelloWorldActivity.this, itemStr); } else if (itemStr.equals("AlertDialog2")){showDialogLayout(HelloWorldActivity.this);} }//=========================AlertDialog==================================================== private void showDialog(Context context, String itemStr) { //AlertAialog的构造函数是protected的,只能通过Builder函数来构建一个新的对象 AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.drawable.ic_launcher); //设置图标 builder.setTitle("我是标题"); //设置标题 builder.setMessage("这里是内容啊啊啊啊!!!");//设置内容 builder.setPositiveButton("Button1", //确认按钮 new DialogInterface.OnClickListener() {//为了方便,不显式声明一个类了 public void onClick(DialogInterface dialog, int whichButton) { setTitle("点击了对话框上的Button1"); } }); builder.setNeutralButton("Button2", //中性按钮 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("点击了对话框上的Button2"); } }); builder.setNegativeButton("Button3", //否认按钮 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("点击了对话框上的Button3"); } }); builder.show(); //显式这个对话框 }//===================基于Layout的AlertDialog================================================ private void showDialogLayout(Context context) { //LayoutInflater的作用是用来动态加载Layout文件的 LayoutInflater inflater = LayoutInflater.from(context); final View textEntryView = inflater.inflate( R.layout.dialog_layout, null);//动态加载Layout文件 final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);//加载之后可以找到其中的控件了 final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setCancelable(false); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("Title"); builder.setView(textEntryView); builder.setPositiveButton("确认", //这里又手动加入了按钮,可以看出,可以混着用的 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(edtInput.getText()); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(""); } }); builder.show(); } } }
2 0
- Android新手入门2016(12)--基于Layout文件的AlertDialog
- Android新手入门2016(11)--非阻塞对话框AlertDialog
- Android的对话框(AlertDialog)
- Android新手入门的十個問題!
- Android Layout文件的属性说明
- android的layout文件命名问题
- Android layout文件中 '?' 的作用
- Android Layout文件的属性说明
- Android新手入门2016(7)--布局
- Android新手入门2016(10)--GridView
- Android新手入门2016(15)--Gallery画廊
- Android新手入门2016(16)--画图
- Android新手入门2016(17)--新手总结
- Android分割layout文件
- android AlertDialog自定义布局文件
- Android AlertDialog添加布局文件
- 【Android】对话框AlertDialog的使用(一)
- android AlertDialog的应用
- 2016.03.22(大水货啊 ,啥都不会)
- java详解单例模式
- Linux系统的休眠与唤醒简介
- 顺时针打印矩阵
- J2EE中MVC的使用以及优化
- Android新手入门2016(12)--基于Layout文件的AlertDialog
- 235. Lowest Common Ancestor of a Binary Search Tree
- module_init 和 late_initcall 区别
- Java程序与mysql的交互
- Mastering R plot -3笔记
- php设计模式学习系列(九)--桥接模式
- Android自定义view-弹出式dialog
- 程序设计
- flume学习之一 是什么,主要应用在什么场景