popupWindow自定义(2)
1、自定义布局(密码框、对话框等等)
2、从下往上弹出效果的实现
一、对话框
第一步:
Xml代码:
第二步:创建SelectPicPopupWindow类继承PopupWindow:
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
//取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//销毁弹出框
dismiss();
}
});
//设置按钮监听
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y=(int) event.getY();
if(event.getAction()==MotionEvent.ACTION_UP){
if(y
第三步:编写MainActivity类实现测试:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//自定义的弹出框类
SelectPicPopupWindow menuWindow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) this.findViewById(R.id.text);
//把文字控件添加监听,点击弹出自定义窗口
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//实例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
//显示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
}
});
}
//为弹出窗口实现监听类
private OnClickListener itemsOnClick = new OnClickListener(){
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.btn_take_photo:
break;
case R.id.btn_pick_photo:
break;
default:
break;
}
}
};
}
二、密码框
自定义密码框xml
PasswordView
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.skypine.skypine.R;
import com.skypine.skypine.ui.MainActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Created by syhuang on 2017/7/19.
*/
public class PasswordView extends RelativeLayout {
/*********************
* 取消
******************/
public interface OnCancelListener {
public void OnCancel();
}
public void setCancelListener(OnCancelListener cancelListener) {
this.cancelListener = cancelListener;
}
OnCancelListener cancelListener;
/***************************************/
private MainActivity mContext;
private String mStringPassword; //输入的密码
private TextView[] mTextViewPsw; // 用数组保存6个TextView
private GridView mGridView; //支付键盘布局
private ArrayList
自定义popwind
使用:
private static PopupWindow mPopupWindow;
private static View popView;
private static PasswordView mPasswordView;
popView = View.inflate(context, R.layout.pop_window, null);
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setTouchable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
// 设置动画
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
mPopupWindow.getContentView().setFocusableInTouchMode(true);
mPopupWindow.getContentView().setFocusable(true);
mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// mPopupWindow.showAsDropDown(controlPager,0,0);
mPasswordView = (PasswordView) popView.findViewById(R.id.pv_pop_win);
// mPasswordView.getTitle().setText(getString(R.string.pin_id));
mPasswordView.setOnFinishInput(new PasswordView.OnPasswordInputFinish() {
@Override
public void inputFinish() {
mPopupWindow.dismiss();
lock.setMove(true);
//验证车辆密码
// checkCarPwd(mPasswordView.getPassword());
if (mPasswordView.getPassword().equals(AppConfig.getInstance().getString("key", ""))) {
//密码正确
listener.onSuccess();
} else {
//密码错误
listener.onError();
}
}
});
mPasswordView.setCancelListener(new PasswordView.OnCancelListener() {
@Override
public void OnCancel() {
lock.setMove(true);
mPopupWindow.dismiss();
}
});
参考:
http://www.tuicool.com/articles/beABNjq
http://104zz.iteye.com/blog/1685389
http://www.jb51.net/article/94888.htm
我的另外一篇文章:
popupWindow自定义(2)