Android新手入门2016(13)--阻塞对话框PopupWindow

上两章都说了非阻塞的对话框,今天说一下阻塞的对话框--PopupWindow

那么什么是阻塞什么是非阻塞呢?PopupWindow和AlertDialog有什么不同呢?

先说AlertDialog,弹出来之后,背面会变灰,并没有阻塞后台的进程,如果没特殊控制,点击后面灰暗处,弹框会消失掉的。

至于PopupWindow,则是弹出来,后面没有任何变化,并且阻塞该应用的进程,如果一直没退出,应用汇一直等待,点击后面也是没有反应的。

不知道为什么现在上传不了图,就不上传了,其实跟AlertDialog一样。

还是继续使用之前的代码来扩充

来看看怎么实现:

新增了一个popup_window.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"
    android:background="#FFFFFF">  

    <TextView android:id="@+id/data_view"
        android:layout_height="wrap_content"
        android:text="我要大保健"
        android:layout_width="fill_parent"/>  

    <EditText android:id="@+id/data_edit"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"    />  

	<LinearLayout android:id="@+id/LinearLayout01"
	    android:layout_height="wrap_content"
	    android:layout_width="fill_parent"
	    android:gravity="center">
	    <Button android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:id="@+id/BtnOK"
	        android:layout_weight="100"
	        android:text="确定">
	    </Button>
	    <Button android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_weight="100"
	        android:text="取消" android:id="@+id/BtnCancel">
	    </Button>
	</LinearLayout>
</LinearLayout>  

声明了一个标题,一个对话框,两个按钮。

然后看实现代码,看过AlertDialog的可以调到下面的代码去。

package com.fable.helloworld;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
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;
        	case 3:
        		imageName = "PopupWindow";//阻塞对话框
        		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);
			}
	    	else if( itemStr.equals("PopupWindow"))
	    	{

	    		showPopupWindow(HelloWorldActivity.this, arg1);
	    	}
    	}
//=========================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();
        }
//===================PopupWindow================================================
        private void showPopupWindow(Context context,View parent) {
        	//LayoutInflater的作用是用来动态加载Layout文件的
            LayoutInflater inflater = LayoutInflater.from(context);
            final View popupView = inflater.inflate( R.layout.popup_window, null);//动态加载Layout文件
            final PopupWindow pWindow = new PopupWindow(popupView,200,200,true);//需要填写宽高,否则显示不了
            final Button button=(Button)popupView.findViewById(R.id.BtnOK);//加载之后可以找到其中的控件了
            button.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    //设置文本框内容
                    EditText edtUsername=(EditText)popupView.findViewById(R.id.data_edit);
                    edtUsername.setText("关注微信:传说之路");
                }
            });
          //Cancel按钮及其处理事件
            Button btnCancel=(Button)popupView.findViewById(R.id.BtnCancel);
            btnCancel.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                	pWindow.dismiss();//关闭
                }
            });
            //显示popupWindow对话框
            pWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);  

        }
    }
}

为了方便对比,这次的代码保留了前两章,

非阻塞对话框AlertDialog基于Layout文件的AlertDialog

加起来就有点多。所以上传了代码,等通过审核后放链接。

时间: 2024-08-08 05:14:52

Android新手入门2016(13)--阻塞对话框PopupWindow的相关文章

Android新手入门2016(16)--画图

本文来自肥宝传说之路,引用必须注明出处! 画图设计到图片的格式,有空可以看看图片资源各种格式.了解一下图片格式,对学习有用的.而且我面试别人的时候也很喜欢问这个问题,哈哈. 先看个图. 直接看代码吧,注释很详细了. activity_hello_world.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andro

Android新手入门2016(11)--非阻塞对话框AlertDialog

写了这么久,看了这么多控件,好像都是静态的,一点交互都没有.这次要弄点弹框,活跃活跃. 这次继续用上一章的代码往下面写吧. 先看看图 还是前一章的九宫图,我把对话框绑定在第一个图标. 点击一下,可以看到如下: 再来看看代码吧 package com.fable.helloworld; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android

Android新手入门2016(7)--布局

布局,这个在服务端变成是没有的,也是服务端的人学习client的一道坎吧. 曾经用cocos2d-x写小游戏的时候就是这个非常难懂,或者能用,可是理解不多的话,非常难写出好的布局,难以适合商业化的应用. 游戏的布局有点像用photoshop画画的感觉.现有一个场景的概念,就像一个画布,然后上面分一层一层.能够单独某一层进行操作.显示的时候,能够觉得画面是从下往上一层一层堆上去.层里面有非常多精灵,精灵就是我们看到的那些会动的人物,建筑,怪什么的.这大概是cocos2d的设计思想吧. 我一直认为,

Android新手入门2016(14)--FragmentTabHost实现选项卡和菜单

本文来自肥宝传说之路,引用必须注明出处! 这章憋了好久.本来想写选项卡的,学到TabHost,TabWidget的,把代码拿过来准备研究的时候,发现竟然在4.0.3版本号被废弃了. 百度一下,发如今后面的版本号,用FragmentTabHost和LayoutInflater来取代了.网上也有一些关于Frame的内容,可是都不是新手教程的. 写得不够通俗.想直接拿代码下来研究,发现竟然非常多人都是上传代码片段,然后再给个收费链接.作为一个穷屌丝,仅仅能自己一点一点去研究了. Frament是什么?

Android新手入门2016(15)--Gallery画廊

本文来自肥宝传说之路,引用必须注明出处! Gallery是Android查看图片的一个工具,用户使用非常方便. 可以通过左右滑动来查看不同的图片 代码比较简单,但是还是搞了一整天,因为碰到了一些问题. 主要是图片的来源问题,这里是通过Java的映射机制和R文件来获得drawable目录下的图片. 不过要注意,drawable类里面是有很多系统本身的属性,有些是不能显示出来的,所以要过滤,否则会报错. 另外放在drawable里面的图片,必须是png格式的.解决这两个问题就好办很多了. activ

Android新手入门2016(8)--ListView之ArrayAdapter

本文来自肥宝传说之路,引用必须注明出处! ListView是Android中经常使用的控件. 什么是列表视图,让我们先看看图: 最常见的样例就是各种菜单的下啦列表. 要实现列表,须要完毕三个要素: 1.ListView 把全部的数据按指定的格式排成列表. 列表中每一项能够称为Item(如上图This is Title). 能够想象得出,要显示列表.就要先弄成相应的格式 2.adapter 适配器就是这样的ListView可以识别的格式,当然适配器有几种.以下再细说.适配器是指定格式的数据.可是我

Android新手入门2016(10)--GridView

本文来自肥宝传说之路.引用必须注明出处! GridView跟ListView一样是多控件布局.实现九宫图是最方便的. 还是先看看图,没图说个鸡鸡是不是 如上图.是一种应用方式.在每一个格子里面.放入应用图标,和显示应用的名字在下方. 以下先看看布局文件: activity_hello_world.xml <? xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="htt

2015年最新Android基础入门教程目录(完结版)

2015年最新Android基础入门教程目录(完结版) 标签(空格分隔): Android基础入门教程 前言: 关于<2015年最新Android基础入门教程目录>终于在今天落下了帷幕,全套教程 共148节已编写完毕,附上目录,关于教程的由来,笔者的情况和自学心得,资源分享 以及一些疑问等可戳:<2015最新Android基础入门教程>完结散花~ 下面是本系列教程的完整目录: 第一章:环境搭建与开发相关(已完结 10/10) Android基础入门教程--1.1 背景相关与系统架构

2015年最新Android基础入门教程目录(临时版)

2015年最新Android基础入门教程目录(临时版) 标签(空格分隔): Android基础入门教程 前言: 嗯,昨晚又给人盗号了,博客上被发表了十几篇黄贴-然后目录给管理误删了,再发一次 后来协商后发现实被设密保问题了,建议各位用csdn的朋友密保自己设置一波~ 密保问题已修改回来了,应该不会再被盗号了-人怕出名猪怕壮哈~下次如果发现博客被封 告知下小猪,如何很急的话可以先到w3c鸟巢菜鸟教程上看Android基础入门教程 经过站长FK进行排版的,可能阅读体验会比csdn好很多!内容基本是同