弹窗类:
public class ButtonPopupWindow extends PopupWindow {
private Button cancelBtn; //弹窗取消按钮
private View menuview;
private LinearLayout llItemContainer;
private RelativeLayout rlRoot;
private Button[] btnItems;
public ButtonPopupWindow(Activity context, int itemNum) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menuview = inflater.inflate(R.layout.bottom_popup_window, null);
findUIControls();
init();
btnItems = new Button[itemNum];
for (int i = 0; i < btnItems.length; i++) {
Button btn = new Button(context);
btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);
btn.setTextSize(16);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = ((int) TypedValue.applyDimension([TypedValue.COMPLEX_UNIT_DIP], 2,
context.getResources().getDisplayMetrics()));
btn.setLayoutParams(layoutParams);
llItemContainer.addView(btn);
btnItems[i] = btn;
}
}
private void findUIControls() {
cancelBtn = menuview.findViewById(R.id.item_popupwindows_cancel); //取消按钮
llItemContainer = menuview.findViewById(R.id.ll_item_container);
rlRoot = menuview.findViewById(R.id.rl_root);
}
private void init() {
/*
* 取消按钮销毁事件
*/
cancelBtn.setOnClickListener(view -> dismiss());
//设置SelectPicPopupWindow的View
this.setContentView(menuview);
//设置SelectPicPopupWindow**弹出窗体的宽
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
//修改高度显示,解决被手机底部虚拟键挡住的问题
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
//this.setAnimationStyle(R.style);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
//menuview添加ontouchlistener监听判断获取触屏位置如果在选择框外面则销毁弹出框
menuview.setOnTouchListener((view, motionEvent) -> {
int height = menuview.findViewById(R.id.ll_popup).getTop();
int y = (int) motionEvent.getY();
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
});
}
/**
* 设置按钮文字
* @param index
* @param text
*/
public void setBtnItemText(int index, String text) {
btnItems[index].setText(text);
}
/**
* 批量设置按钮文字
* @param texts
*/
public void setBtnItemTexts(String[] texts) {
int n = Math.min(texts.length, btnItems.length);
for (int i = 0; i < n; i++) {
btnItems[i].setText(texts[i]);
}
}
/**
* 设置按钮点击事件
* @param index
* @param listener
*/
public void setBtnItemClickListener(int index, View.OnClickListener listener) {
btnItems[index].setOnClickListener(listener);
}
/**
* 设置取消按钮是否可见
* @param visiable
*/
public void setCancelBtnVisiable(boolean visiable) {
if (visiable) {
cancelBtn.setVisibility(View.VISIBLE);
} else {
cancelBtn.setVisibility(View.GONE);
}
}
/**
* 设置按钮字体颜色
* @param color
*/
public void setBtnItemsTextColor(int color) {
for (int i = 0; i < btnItems.length; i++) {
btnItems[i].setTextColor(color);
}
}
/**
* 设置按钮字体大小
* @param size
*/
public void setBtnitemsTextSize(float size) {
for (int i = 0; i < btnItems.length; i++) {
btnItems[i].setTextSize(size);
}
}
/**
* 设置取消按钮字体大小
* @param size
*/
public void setCancelBtnTextSize(float size) {
cancelBtn.setTextSize(size);
}
/**
* 设置取消按钮颜色
* @param color
*/
public void setCancelBtnTextColor(int color) {
cancelBtn.setTextColor(color);
}
/**
* 设置取消按钮文字
* @param text
*/
public void setCancelBtnText(String text) {
cancelBtn.setText(text);
}
}
效果如图:
image.png
网友评论