自定义View之PopupWindow的使用

时间:2023-02-09 15:07:16

PopupWindow是Android上自定义弹出窗口,使用起来很方便。

PopupWindow的构造函数为

public PopupWindow(View contentView, int width, int height, boolean focusable)

contentView为要显示的view,width和height为宽和高,值为像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

focusable为是否可以获得焦点,这是一个很重要的参数,也可以通过

public void setFocusable(boolean focusable)

来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。

如果PopupWindow中有Editor的话,focusable要为true。


我们在popupwindow中包含一个listView作为他的子View

首先上mainactivity

package com.example.pullup;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView upimage;
private EditText et_1;
private ListView listView;
private ArrayList<String> arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

upimage= (ImageView) findViewById(R.id.upimage);
et_1= (EditText) findViewById(R.id.et_1);
upimage.setOnClickListener(this);
initdata();
listView=new ListView(this);
listView.setAdapter(new Myadpter(this,arr,et_1));

}

private void initdata() {
arr=new ArrayList<>();
for (int i=0;i<10;i++){
arr.add("aaaa"+i);
}


}
private PopupWindow popupWindow;
@Override
public void onClick(View v) {
//点击后构造popupwindow
if (popupWindow==null) {
popupWindow = new PopupWindow();
popupWindow.setWidth(et_1.getWidth());
popupWindow.setHeight(200);
popupWindow.setContentView(listView);
/*
* 这三行是作用是点击别的地方隐藏popupwindows
* */
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),(Bitmap) null));

}
//在et_1的下方显示
popupWindow.showAsDropDown(et_1,0,0);
/*
还有其他显示方式:
自定义View之PopupWindow的使用
*/
}
}

再上我们Listview的Adapter
package com.example.pullup;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by yang on 2017/7/3.
*/
public class Myadpter extends BaseAdapter{

private ArrayList<String> arr;
private Context Mycontext;
private TextView textView;

private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle= msg.getData();
String text=bundle.getString("data");
textView.setText(text);
}
};

public Myadpter(Context contexts,ArrayList<String> arr,TextView textView){
this.arr=arr;
this.Mycontext=contexts;
this.textView=textView;
}
@Override
public int getCount() {
return arr.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) {

View view;
final ViewHoder viewHoder;
if (convertView==null){
view= View.inflate(Mycontext,R.layout.listview_items,null);
viewHoder=new ViewHoder();
viewHoder.imageView1= (ImageView) view.findViewById(R.id.iv_1);
viewHoder.textView= (TextView) view.findViewById(R.id.tv_1);
viewHoder.imageView2= (ImageView) view.findViewById(R.id.iv_2);
view.setTag(viewHoder);
}else {
view= convertView;
viewHoder= (ViewHoder) view.getTag();
}
viewHoder.textView.setText(arr.get(position));

viewHoder.imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arr.remove(position);
notifyDataSetChanged();
}
});
viewHoder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg=new Message();
Bundle bundle=new Bundle();
bundle.putString("data",viewHoder.textView.getText().toString().trim());
msg.setData(bundle);
handler.sendMessage(msg);
}
});
return view;

}
class ViewHoder{
private ImageView imageView1;
private TextView textView;
private ImageView imageView2;
}
}

最后上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"
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="com.example.pullup.MainActivity">

<EditText
android:id="@+id/et_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:paddingRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:maxLines="1" />
<ImageView
android:id="@+id/upimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/et_1"
android:layout_alignTop="@id/et_1"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/down_arrow"
/>
</RelativeLayout>


功能使用:

点击图片弹出popupwindow

点击listview的删除按钮删除条目:

arr.remove(position);
notifyDataSetChanged();

点击listview的内容显示在EditView中

这里用Handler实现


上效果图:

自定义View之PopupWindow的使用

自定义View之PopupWindow的使用