PopupWindow踩过的坑

来源:互联网 发布:淘宝怎么卖游戏账号 编辑:程序博客网 时间:2024/04/28 14:54

PopupWindow是Android上自定义的弹出界面,方便用户的自定义样式。

点进popupwindow的源码中你会看到有这样一个构造函数的,里面的参数分别是(引用的布局即显示的view,宽,高,焦点)focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过

public void setFocusable(boolean focusable) {    throw new RuntimeException("Stub!");}

来设置焦点。如果将focusable设为false则popupwindow没有了焦点在其上面进行不了点击操作,如果为true则可以对其所有的触屏和物理按键都有PopupWindows处理。如果PopupWindow中有输入操作的话(如editor),focusable要为true。

public PopupWindow(View contentView, int width, int height, boolean focusable) {    throw new RuntimeException("Stub!");}

注释点击popupwindow主要布局(one)之外的部分窗口关闭,
                广播部分可以不看主要是发送消息注销登录;




创建对话框方法dialog
    protected void dialog() {


        View popuviewView=View.inflate(this, R.layout.out_popup, null);
        View popuone=popuviewView.findViewById(R.id.one);
        final LinearLayout outLayout=(LinearLayout) popuviewView.findViewById(R.id.out);

    final PopupWindow popupWindow=new PopupWindow(popuviewView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    //取消退出(关闭对话框)
    popup_qx=(Button) popuviewView.findViewById(R.id.pop_quxiao);
    popup_qx.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    popup_x=(ImageView) popuviewView.findViewById(R.id.out_popup_x);
    popup_x.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    //确认退出(退出登录)
    popup_qd=(Button) popuviewView.findViewById(R.id.pop_queding);
    popup_qd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplication(), "quedeing", Toast.LENGTH_SHORT).show();
            Intent loginIntent=new Intent(getApplication(), LoginActivity.class);
            startActivity(loginIntent);

            popupWindow.dismiss();
            /**
             * 通过广播(BroadcastReceiver)注销登录
             * 1.点击退出按钮向MianActivity发送广播
             * 2.关闭当前的acticity;
             */
            Intent mainIntent=new Intent();
            mainIntent.setAction("com.anxun.bqbh.activity");//添加action属性
            mainIntent.putExtra("bqbh", "finish");
            sendBroadcast(mainIntent);//发送消息(接收方在MianActivity到登录界面)
            SetActivity.this.finish();//关闭Setactivity

        }
    });
//    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
     ColorDrawable dw = new ColorDrawable(-00000);
     popupWindow.setBackgroundDrawable(dw);
     popuone.setFocusable(true);
     popupWindow.setOutsideTouchable(false);
    popupWindow.showAtLocation(setLayout, Gravity.CENTER, 0, 0);
//    popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    popuone.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //按下
            if (event.getAction()==MotionEvent.ACTION_DOWN) {
                outLayout.setEnabled(false);//不可激活
                Toast.makeText(getApplication(), "按下", Toast.LENGTH_SHORT).show();
            }
            //抬起
            if (event.getAction()==MotionEvent.ACTION_UP) {
                outLayout.setEnabled(true);//可激活
                Toast.makeText(getApplication(), "抬起", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });

    outLayout.setOnClickListener(new OnClickListener() {

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

out_popup.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b0000000"
    >

     <LinearLayout 
        android:id="@+id/out"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="horizontal">

    </LinearLayout>
    <LinearLayout
        android:id="@+id/one"
        android:layout_centerInParent="true"
        android:layout_width="300dp"
        android:layout_height="180dp"
        android:background="@drawable/out_shape"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/tixingkuang" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="温馨提示"
                android:textColor="#333333"
                android:textSize="17sp" />

            <ImageView
                android:id="@+id/out_popup_x"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:src="@drawable/x" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            >
            <TextView
                android:id="@+id/textone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="您确定要退出登录吗"
                android:layout_marginTop="10dp"
                android:textColor="#333333"
                android:textSize="17sp" />
            <RelativeLayout
                android:layout_centerHorizontal="true"
                android:layout_below="@id/textone" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

            <!-- 取消按钮 -->

            <Button
                android:id="@+id/pop_quxiao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="23dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="16dp"
                android:background="@drawable/tanchu_qx" />

            <!-- 确定按钮 -->
            <Button
                android:id="@+id/pop_queding"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="23dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="16dp"
                android:background="@drawable/tanchu_q" />
           </RelativeLayout>

        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>
注释点击popupwindow主要布局(one)之外的部分窗口关闭,
                广播部分可以不看主要是发送消息注销登录;




创建对话框方法dialog
    protected void dialog() {


        View popuviewView=View.inflate(this, R.layout.out_popup, null);
        View popuone=popuviewView.findViewById(R.id.one);
        final LinearLayout outLayout=(LinearLayout) popuviewView.findViewById(R.id.out);

    final PopupWindow popupWindow=new PopupWindow(popuviewView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    //取消退出(关闭对话框)
    popup_qx=(Button) popuviewView.findViewById(R.id.pop_quxiao);
    popup_qx.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    popup_x=(ImageView) popuviewView.findViewById(R.id.out_popup_x);
    popup_x.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    //确认退出(退出登录)
    popup_qd=(Button) popuviewView.findViewById(R.id.pop_queding);
    popup_qd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplication(), "quedeing", Toast.LENGTH_SHORT).show();
            Intent loginIntent=new Intent(getApplication(), LoginActivity.class);
            startActivity(loginIntent);

            popupWindow.dismiss();
            /**
             * 通过广播(BroadcastReceiver)注销登录
             * 1.点击退出按钮向MianActivity发送广播
             * 2.关闭当前的acticity;
             */
            Intent mainIntent=new Intent();
            mainIntent.setAction("com.anxun.bqbh.activity");//添加action属性
            mainIntent.putExtra("bqbh", "finish");
            sendBroadcast(mainIntent);//发送消息(接收方在MianActivity到登录界面)
            SetActivity.this.finish();//关闭Setactivity

        }
    });
//    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
     ColorDrawable dw = new ColorDrawable(-00000);
     popupWindow.setBackgroundDrawable(dw);
     popuone.setFocusable(true);
     popupWindow.setOutsideTouchable(false);
    popupWindow.showAtLocation(setLayout, Gravity.CENTER, 0, 0);
//    popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    popuone.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //按下
            if (event.getAction()==MotionEvent.ACTION_DOWN) {
                outLayout.setEnabled(false);//不可激活
                Toast.makeText(getApplication(), "按下", Toast.LENGTH_SHORT).show();
            }
            //抬起
            if (event.getAction()==MotionEvent.ACTION_UP) {
                outLayout.setEnabled(true);//可激活
                Toast.makeText(getApplication(), "抬起", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });

    outLayout.setOnClickListener(new OnClickListener() {

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

out_popup.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b0000000"
    >

     <LinearLayout 
        android:id="@+id/out"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="horizontal">

    </LinearLayout>
    <LinearLayout
        android:id="@+id/one"
        android:layout_centerInParent="true"
        android:layout_width="300dp"
        android:layout_height="180dp"
        android:background="@drawable/out_shape"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/tixingkuang" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="温馨提示"
                android:textColor="#333333"
                android:textSize="17sp" />

            <ImageView
                android:id="@+id/out_popup_x"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:src="@drawable/x" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            >
            <TextView
                android:id="@+id/textone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="您确定要退出登录吗"
                android:layout_marginTop="10dp"
                android:textColor="#333333"
                android:textSize="17sp" />
            <RelativeLayout
                android:layout_centerHorizontal="true"
                android:layout_below="@id/textone" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

            <!-- 取消按钮 -->

            <Button
                android:id="@+id/pop_quxiao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="23dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="16dp"
                android:background="@drawable/tanchu_qx" />

            <!-- 确定按钮 -->
            <Button
                android:id="@+id/pop_queding"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="23dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="16dp"
                android:background="@drawable/tanchu_q" />
           </RelativeLayout>

        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

0 0
原创粉丝点击