popupwindows

 

<1>.能够点击popupwindow 内部空间,外部触摸消失,外部点击事件。

package myapplication.com.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements PopupWindow.OnDismissListener {

    TextView textView_type;
    int state = 0;
    PopupWindow pop;
    View layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView_type = (TextView) findViewById(R.id.click);
        popshop();
        //  popupwindows 外部的点击事件
        TextView textView = (TextView) findViewById(R.id.text);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "外部点击", Toast.LENGTH_SHORT).show();
            }
        });


    }


    private void popshop() {
        // textView_type= (TextView) view.findViewById(R.id.type_textview);
        // mClassifyPopupWindow.showAtLocation(getActivity().
        // findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);


        textView_type.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (state == 1 && pop.isShowing()) {
                    state = 0;
                    pop.dismiss();
                } else {
                    /*** 弹出自定义的菜单 ***/
                    int height;
                    Display defaultDisplay = getWindowManager().getDefaultDisplay();
                    height = defaultDisplay.getHeight() / 10 * 7;
                    layout = View.inflate(MainActivity.this, R.layout.popwindow_layout, null);
                    //popweindows 里面可点击
                    TextView pop_textview1 = (TextView) layout.findViewById(R.id.pop_textview1);
                    pop_textview1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pop.dismiss();
                        }
                    });
                    pop = new PopupWindow(layout, defaultDisplay.getWidth(),
                            height);
                    // ColorDrawable cd = new ColorDrawable(-0000);
                    //  pop.setBackgroundDrawable(cd);
                    // pop.showAsDropDown(v);

                    pop.update();
                    pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    pop.setTouchable(true);        // 设置popupwindow可点击
                    pop.setOutsideTouchable(true); // 设置popupwindow外部可点击
                    //  pop.setFocusable(true);    // 获取焦点 这时外部不可点击触摸
                    /* 设置popupwindow的位置 */
                    pop.showAtLocation(layout,
                            (Gravity.BOTTOM - textView_type.getHeight())
                                    | Gravity.LEFT, 0, 2 * textView_type.getHeight());
                    state = 1;
                    pop.setTouchInterceptor(new View.OnTouchListener() {
                        public boolean onTouch(View v, MotionEvent event) {

                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                pop.dismiss();
                                return true;
                            }
                            return false;
                        }

                    });


                }
            }
        });

    }

    @Override
    public void onDismiss() {
        pop.dismiss();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

        if (pop != null && pop.isShowing()) {

            pop.dismiss();

            pop = null;

        }

        return super.onTouchEvent(event);

    }

}

<2>.Mainacitivty.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="myapplication.com.myapplication.MainActivity">

    <TextView
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="点击弹出PopWindows" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="pop弹出时仍能点击" />
</LinearLayout>

<3>. popwindow_layout

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

    android:orientation="vertical">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="popwindows内部点击事件"
        android:id="@+id/pop_textview1"
        android:textSize="22dp"
        android:gravity="center_horizontal"/>
    <View
        android:layout_height="2dp"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:background="#000000"

        />

      <myapplication.com.myapplication.XCFlowLayout
            android:id="@+id/flowlayout"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >

         </myapplication.com.myapplication.XCFlowLayout>


</LinearLayout>

 <4> 自定义菜单栏

pop的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#6e6e6e"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:drawableLeft="@mipmap/yun"
        android:gravity="center_horizontal|center_vertical"
        android:text="菜单"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:drawableLeft="@mipmap/yun"
        android:gravity="center_horizontal|center_vertical"
        android:text="收藏"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:drawableLeft="@mipmap/yun"
        android:gravity="center_horizontal|center_vertical"
        android:text="爱好"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>
View Code

方法实现:

  /**
     *
     * @param view  view点击事件,弹出popupwindow,view传入作为参数
     */
    private void showPopupWindow_dingbu(View view) {

        /**
         * 一个自定义的布局,作为显示的内容
         */

        View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout, null);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2;

        TextView textView3 = (TextView) contentView.findViewById(R.id.textView1);
        /**
         * 设置按钮的点击事件
         */
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });




        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });

        popupWindow.showAtLocation(view, Gravity.TOP|Gravity.RIGHT, 0, 140);  //右下
        popupWindow.showAsDropDown(view);
    }

 

posted @ 2016-10-13 14:03  咖喱不见不散啊  阅读(211)  评论(0编辑  收藏  举报