PopupWindow(一)

来源:互联网 发布:建一个淘宝白菜群佣金 编辑:程序博客网 时间:2024/05/02 03:17

From:http://blog.csdn.net/scyatcs/article/details/7929941

理论部分

一、PopupWindow和AlertDialog都是Android对话框的内容

二、两者的区别和联系

       本质区别为:AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。这两种区别的表现是:AlertDialog弹出时,背景是黑色的,但是当我们点击背景,AlertDialog会消失,证明程序不仅响应AlertDialog的操作,还响应其他操作,其他程序没有被阻塞,这说明了AlertDialog是非阻塞式对话框;PopupWindow弹出时,背景没有什么变化,但是当我们点击背景的时候,程序没有响应,只允许我们操作PopupWindow,其他操作被阻塞。

实践部分

一、创建PopupWindow的核心代码:

       PopupWindow pw = new PopupWindow(view,width,height);

       pw.setContentView(popupconten);//重新设置PopupWindow的内容

       pw.setFocusable(true);//默认是false,为false时,PopupWindow没有获得焦点能力,如果这是PopupWindow的内容中有EidtText,需要输入,这是是无法输入的;只有为true的时候,PopupWindow才具有获得焦点能力,EditText才是真正的EditText。

      pw.setAsDropDown(View view);//设置PopupWindow弹出的位置。

二、实例

<?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="fill_parent"
    android:orientation="vertical"
    android:id="@+id/mainlayout" >
    <TextView android:layout_width="fill_parent"
        android:id="@+id/textview1"
        android:layout_height="wrap_content"
        android:text="欢迎信息"/>
    <Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击弹出对话框"/>
</LinearLayout>

 

<?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="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EditText" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1_sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"/>

        <Button android:id="@+id/button2_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            />
    </LinearLayout>

</LinearLayout>

 

 

package com.jiankeyan;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * 新建一个popupWindow弹出框 popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,
 *
 * 这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。
 *
 *
 */

public class PopupWindowActivity extends Activity {
 Button button;
 TextView textView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) findViewById(R.id.button1);
  textView = (TextView) findViewById(R.id.textview1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    initPopWindow();
   }

  });
 }

 private void initPopWindow() {
  // 加载PopupWindow的布局文件
  View contentView = LayoutInflater.from(getApplicationContext())
    .inflate(R.layout.popup, null);
  // 设置PopupWindow的背景颜色
  contentView.setBackgroundColor(Color.RED);
  // 声明一个对话框
  final PopupWindow popupWindow = new PopupWindow(null, 200, 300);
  // 为自定义的对话框设置自定义布局
  popupWindow.setContentView(contentView);

  // 这个popupWindow.setFocusable(true);非常重要,如果不在弹出之前加上这条语句,你会很悲剧的发现,你是无法在
  //
  // editText中输入任何东西的。该方法可以设定popupWindow获取焦点的能力。当设置为true时,系统会捕获到焦点给popupWindow
  //
  // 上的组件。默认为false哦.该方法一定要在弹出对话框之前进行调用。
  popupWindow.setFocusable(false);

  // popupWindow.showAsDropDown(View view)弹出对话框,位置在紧挨着view组件
  //
  // showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y
  // 代表着偏移量
  //
  // showAtLocation(View parent, int gravity, int x, int y)弹出对话框
  //
  // parent 父布局 gravity 依靠父布局的位置如Gravity.CENTER x y 坐标值

  popupWindow.showAsDropDown(button);

  final EditText editText = (EditText) contentView
    .findViewById(R.id.editText1);
  // 设定当你点击EditText时,弹出的输入框是啥样子的。这里设置默认数字是输入,非数字不能输入
  editText.setInputType(InputType.TYPE_CLASS_NUMBER);

  Button button_sure = (Button) contentView
    .findViewById(R.id.button1_sure);
  button_sure.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    popupWindow.dismiss();
    textView.setText("展示信息:" + editText.getText());
   }
  });
  Button button_cancel = (Button) contentView
    .findViewById(R.id.button2_cancel);
  button_cancel.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    popupWindow.dismiss();
   }
  });
 }
}

 

 

 

0 0
原创粉丝点击