android中的PopupWindow的使用

PopupWindow有点类似于Dialog,相同点在于都是弹出窗口,并且都可以对其进行自定义显
示,并且里面的监听组件,进行相应的操作,但它与Dialog又有很大的区别,PopupWindow
只是弹出窗口,不会使宿主Activity组件失去焦点,也就是说PopupWindow弹出后,你仍可
以与宿主Activity进行交互,Dialog却不能做到这一点

PopupWindow有几种构造方法,无参数的我们一般不使用,常用的为
public PopupWindow(Context context);
 //给定一个上下文,当我们设置了setOutsideTouchable为true时,在触摸到弹框外部后自
动关闭。但是这个构造方法必须去指定显示的布局,布局的宽高,焦点。

 public PopupWindow(View contentView, int width, int height, boolean
focusable)
 //给定一个布局。宽,高和焦点,但是在了setOutsideTouchable为true时,触摸到弹框外
部也不能自动关闭,必须去指定背景,一般使用window.setBackgroundDrawable(new ColorDrawable(0x00000000))

常用方法

setOutsideTouchable(boolean);//指定设置显示PopuWindow之后在外面点击是否有效。
 setFocusable(boolean);//指定是否获取焦点

 setBackgroundDrawable(new ColorDrawable(0));//没有上下文时必须指定,否者setOutsideTouchable无效

 showAsDropDown(anchor, 0, 0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量

 showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);
 //(以某个View为参考),表示弹出窗口以parent组件为参考,位于左侧,偏移-90。

 setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//设置窗口消失事件

下面一个示例效果图如下:

这里写图片描述

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    List<String> numbers = new ArrayList<>();
    ImageView iv_down;
    EditText et_content;
    RelativeLayout rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i = 0; i < 50; i++) {
            numbers.add("13815474" + i);
        }
        iv_down = (ImageView) findViewById(R.id.iv_down);
        et_content = (EditText) findViewById(R.id.et_content);
        rl = (RelativeLayout) findViewById(R.id.rlS);
        iv_down.setOnClickListener(this);
    }

    PopupWindow window;

    @Override
    public void onClick(View v) {
        ListView lv = new ListView(this);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                et_content.setText(numbers.get(position));
                window.dismiss();
            }
        });
        lv.setAdapter(adapter);
        window = new PopupWindow(lv, rl.getWidth(), 200, true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.showAsDropDown(rl, 0, 50);
    }

    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return numbers.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHodler holder;
            if (convertView == null) {
                convertView = View.inflate(getBaseContext(), R.layout.iten_layout, null);
                holder = new ViewHodler(convertView);
                convertView.setTag(holder);
            } else
                holder = (ViewHodler) convertView.getTag();
            holder.tv_phone.setText(numbers.get(position));
            //给一个删除数据的事件
            holder.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    numbers.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
//            holder.tv_phone.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    et_content.setText(numbers.get(position));
//                    window.dismiss();
//                }
//            });
            return convertView;
        }

        class ViewHodler {
            TextView tv_phone;
            ImageView iv_delete;

            public ViewHodler(View convertView) {
                tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
                iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            }
        }
    };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.popupwindowdemo.MainActivity">


    <RelativeLayout
        android:id="@+id/rlS"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="50dp"
        android:background="@drawable/back"
        android:padding="5dp">

        <EditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@null"
            android:hint="请输入电话号码" />

        <ImageView
            android:id="@+id/iv_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/down_arrow" />
    </RelativeLayout>

</RelativeLayout>

iten_layout.xml

<?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:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:src="@mipmap/user" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="电话" />

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:src="@mipmap/delete" />
</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值