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

来源:互联网 发布:安徽省大数据产业协会 编辑:程序博客网 时间:2024/05/17 23:09

本文来自肥宝传说之路,引用必须注明出处!

上两章都说了非阻塞的对话框,今天说一下阻塞的对话框--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

加起来就有点多。所以上传了代码,点击进入下载页面。资源是免费的,本来就不是什么高端东西。

昨晚发现很多博客都是写着博客,连过去资源那边收费,太坑了



1 1
原创粉丝点击