• Android之PopupWindow弹出对话框


    Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:

                 

    函数 简介
    showAsDropDown(View anchor) 相对某个控件的位置(正左下方),无偏移
    showAsDropDown(View anchor, int xoff, int yoff) 相对某个控件的位置,有偏移
    showAtLocation(View parent, int gravity, int x, int y) 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等

    下面是运行程序截图:

                  

         

                  

          程序代码:

          布局:main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
    android:id="@+id/tv_showText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:text="@string/hello"
    android:textSize="22px"
    />

    <Button
    android:id="@+id/bt_PopupWindow1"
    android:text="以自己为Anchor,不偏移"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />

    <Button
    android:id="@+id/bt_PopupWindow2"
    android:text="以自己为Anchor,正下方"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    /> 

    <Button
    android:id="@+id/bt_PopupWindow3"
    android:text="以屏幕中心为参照,不偏移(正中间)"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />

    <Button
    android:id="@+id/bt_PopupWindow4"
    android:text="以屏幕下方为参照,下方中间"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    /> 


    </LinearLayout>

       自定义对话框dialog.xml

    <?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"
    >
    <TextView 
    android:id="@+id/tv_tip"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="请输入内容:"
    />

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

    <LinearLayout
    android:gravity="center_horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    <Button
    android:id="@+id/bt_ok"
    android:text="确定"
    android:layout_width="100px" 
    android:layout_height="50px"
    />

    <Button
    android:id="@+id/bt_cancle"
    android:text="取消"
    android:layout_width="100px" 
    android:layout_height="50px"
    /> 
    </LinearLayout>
    </LinearLayout>

     代码:

    package com.myandroid.test;

    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Gallery;
    import android.widget.PopupWindow;
    import android.widget.TextView;

    public class PopupWindowTest extends Activity {//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。
    private Button bt_popupWindow1;
    private Button bt_popupWindow2;
    private Button bt_popupWindow3;
    private Button bt_popupWindow4;
    private TextView tv_showText;
    private PopupWindow popupWindow;
    private int screenWidth;
    private int screenHeight;
    private int dialgoWidth;
    private int dialgoheight;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initView();
    }

    /**
    * 初始化控件和响应事件
    */
    private void initView() {
    bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);
    bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);
    bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3);
    bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4);
    tv_showText = (TextView)findViewById(R.id.tv_showText);

    bt_popupWindow1.setOnClickListener(new ClickEvent());
    bt_popupWindow2.setOnClickListener(new ClickEvent());
    bt_popupWindow3.setOnClickListener(new ClickEvent());
    bt_popupWindow4.setOnClickListener(new ClickEvent());



    }

    /**
    * 按钮点击事件处理
    * @author Kobi
    *
    */
    private class ClickEvent implements OnClickListener {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()) {

    case R.id.bt_PopupWindow1: //以自己为Anchor,不偏移
    getPopupWindow();
    popupWindow.showAsDropDown(v);
    break;

    case R.id.bt_PopupWindow2: //以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方
    getPopupWindow();
    popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/2, 0);
    break;

    case R.id.bt_PopupWindow3: //以屏幕中心为参照,不偏移
    getPopupWindow();
    popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 0, 0);
    break;

    case R.id.bt_PopupWindow4: //以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2, 0) --屏幕下方中央
    getPopupWindow();
    popupWindow.showAtLocation(findViewById(R.id.layout), 
    Gravity.BOTTOM, 0, 0);
    break;

    default:
    break;
    }
    }

    }

    /**
    * 创建PopupWindow
    */
    protected void initPopuptWindow() {
    // TODO Auto-generated method stub


    View popupWindow_view = getLayoutInflater().inflate( //获取自定义布局文件dialog.xml的视图
    R.layout.dialog, null,false);

    popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);//创建PopupWindow实例

    Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok); //dialog.xml视图里面的控件
    Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle);
    final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text);

    bt_ok.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    tv_showText.setText(et_text.getText()); //在标签里显示内容
    popupWindow.dismiss(); //对话框消失
    }
    });

    bt_cancle.setOnClickListener(new OnClickListener() {

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

    //获取屏幕和对话框各自高宽
    screenWidth = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
    screenHeight = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();

    dialgoWidth = popupWindow.getWidth();
    dialgoheight = popupWindow.getHeight();

    }

    /*
    * 获取PopupWindow实例
    */
    private void getPopupWindow() {

    if(null != popupWindow) {
    popupWindow.dismiss();
    return;
    }else {
    initPopuptWindow();
    }
    }

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.e("ActivityState", "onPause");
    }

    @Override
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.e("ActivityState", "onResume");
    }


    }

  • 相关阅读:
    什么是tomcat集群?
    cmd黑客入侵命令大全
    Linix基本命令
    Windows CMD命令大全
    python 函数1
    Python 集合(set)使用
    python 数据字典应用
    python 数据运算
    python 数据类型(元组(不可变列表),字符串
    python 数据类型(列表)学习笔记
  • 原文地址:https://www.cnblogs.com/nan325/p/3026896.html
Copyright © 2020-2023  润新知