做项目经常需要用到各种各样的提示框,今天先把通用的提示框和仿IOS的弹出框写到demo里面后面会持续更新其他的。
1.提示框效果
2.仿IOS弹出框效果
Activity代码
代码语言:javascript复制package com.example.iosdialog;
import com.example.iosdialog.IosDialog.MyPopClickListener;
import com.example.iosdialog.R;
import com.example.iosdialog.TipsDialog.OnMySimpleDialogListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.tips_dialog_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showTipsDialog();
}
});
findViewById(R.id.ios_dialog_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showIosDialog();
}
});
}
private void showTipsDialog() {
new TipsDialog(MainActivity.this, "温馨提示", "缺少系统数据库,是否下载?", "稍后下载", "下载", false).SetOnMySimpleDialogListener(new OnMySimpleDialogListener() {
@Override
public void onMySure() {
}
@Override
public void onMyCancle() {
}
});
}
private void showIosDialog() {
new IosDialog(MainActivity.this, "取消",
new String[] { "孙悟空", "猪八戒", "沙僧" }, true)
.setOnMyPopClickListener(new MyPopClickListener() {
@Override
public void myListItemClick(int itemPosition,String itemContent) {
if (itemPosition == 0) {
Toast.makeText(MainActivity.this, "点击了孙悟空", Toast.LENGTH_SHORT).show();
} else if (itemPosition == 1) {
Toast.makeText(MainActivity.this, "点击了猪八戒", Toast.LENGTH_SHORT).show();
} else if (itemPosition == 2) {
Toast.makeText(MainActivity.this, "点击了沙僧", Toast.LENGTH_SHORT).show();
}
}
@Override
public void myCancleClick(String cancleContent) {
}
});
}
}
IOS弹框
代码语言:javascript复制package com.example.iosdialog;
import com.example.iosdialog.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
public class IosDialog {
private Context mContext;
private String mCancleContent;
private String[] mItems;
private Boolean mCancleAbleOutSide = true;
private PopupWindow mPopupWindow;
private View mShowAtView = null ;
/**
*
* @param mContext 上下文
* @param mCancleContent 取消按钮文字
* @param mItems 文字菜单数组
* @param mCancleAbleOutSide 是否让点击外部取消
*/
public IosDialog(Context mContext,String mCancleContent, String[] mItems, Boolean mCancleAbleOutSide) {
super();
this.mContext = mContext;
this.mCancleContent = mCancleContent;
this.mItems = mItems;
this.mCancleAbleOutSide = mCancleAbleOutSide;
showMyBottomPop();
}
/**
* 此种情况适用于在弹框上弹出pop
* @param mShowAtView 弹框的布局
* @param mContext 上下文
* @param mCancleContent 取消按钮文字
* @param mItems 文字菜单数组
* @param mCancleAbleOutSide 是否让点击外部取消
*/
public IosDialog(View mShowAtView ,Context mContext,String mCancleContent, String[] mItems, Boolean mCancleAbleOutSide) {
super();
this.mContext = mContext;
this.mCancleContent = mCancleContent;
this.mItems = mItems;
this.mCancleAbleOutSide = mCancleAbleOutSide;
this.mShowAtView = mShowAtView;
showMyBottomPop();
}
@SuppressWarnings("deprecation")
@SuppressLint("InflateParams")
public void showMyBottomPop() {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_ios_pop, null);
ListView list = (ListView) view.findViewById(R.id.pop_list);
TextView cancle = (TextView) view.findViewById(R.id.cancle_tv);
cancle.setText(mCancleContent);
list.setAdapter(new MyItemAdapter());
mPopupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setFocusable(true);
// 点击外面popupWindow消失
mPopupWindow.setOutsideTouchable(mCancleAbleOutSide);
if (mCancleAbleOutSide) {
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
}
// 设置popWindow的显示和消失动画
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
if (mShowAtView!=null) {
mPopupWindow.showAtLocation(mShowAtView, Gravity.BOTTOM, 0, 0);
}else {
mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
backgroundAlpha(0.5f);
cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
disMissPop();
if (myPopClickListener!=null) {
myPopClickListener.myCancleClick(mCancleContent);
}
}
});
mPopupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
}
class MyItemAdapter extends BaseAdapter{
@Override
public int getCount() {
int conut = 0;
if (mItems!=null && mItems.length>0) {
conut = mItems.length;
}
return conut;
}
@Override
public Object getItem(int position) {
String item = "暂无内容";
if (mItems!=null && mItems.length>0) {
item = mItems[position];
}
return item;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder= new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_list_item, parent , false);
holder.item_tv = (TextView) convertView.findViewById(R.id.item_tv);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.item_tv.setText(getItem(position).toString());
if (getCount()==1) {
convertView.setBackgroundResource(R.drawable.pop_cancle);
}else {
if (position == 0) {
convertView.setBackgroundResource(R.drawable.pop_item_first_bg);
}else if (position == (mItems.length - 1)) {
convertView.setBackgroundResource(R.drawable.pop_item_last_bg);
}else {
convertView.setBackgroundResource(R.drawable.pop_item_middle_bg);
}
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
disMissPop();
if (myPopClickListener!=null) {
myPopClickListener.myListItemClick(position, getItem(position).toString());
}
}
});
return convertView;
}
class ViewHolder {
TextView item_tv;
}
}
private MyPopClickListener myPopClickListener;
public void setOnMyPopClickListener(MyPopClickListener myPopClickListener){
this.myPopClickListener = myPopClickListener;
}
public interface MyPopClickListener{
public void myListItemClick(int itemPosition, String itemContent);
public void myCancleClick(String cancleContent);
}
private void disMissPop(){
if (mPopupWindow!=null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
lp.alpha = bgAlpha; // 0.0-1.0
((Activity) mContext).getWindow().setAttributes(lp);
}
}
通用提示框代码
代码语言:javascript复制package com.example.iosdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.text.util.Linkify;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class TipsDialog implements OnClickListener {
private AlertDialog mDialog;
private Context mContext;
public TextView mTitleTv , mContentTv;
private Button mCancleBtn , mSureBtn;
private String mTextTitle,mTextContent, mTextCancle, mTextSure;
private boolean mCanceledOnTouchOutside = true;
/**
* @param context 上下文
* @param textTitle 标题
* @param textContent 内容
* @param textCancle 取消
* @param textSure 确定
* @param canceledOnTouchOutside 能否点击外部消失弹框
*/
public TipsDialog(Context context , String textTitle,String textContent, String textCancle, String textSure , boolean canceledOnTouchOutside) {
this.mContext = context;
this.mTextTitle = textTitle;
this.mTextContent = textContent;
this.mTextCancle = textCancle;
this.mTextSure = textSure;
this.mCanceledOnTouchOutside = canceledOnTouchOutside;
createDialog();
}
private void createDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_simple_dialog, null);
mTitleTv = (TextView) view.findViewById(R.id.dialog_tv_title);
mContentTv = (TextView) view.findViewById(R.id.dialog_tv_content);
mCancleBtn = (Button) view.findViewById(R.id.dialog_btn_cancle);
mSureBtn = (Button) view.findViewById(R.id.dialog_btn_sure);
mContentTv.setAutoLinkMask(Linkify.ALL);
mCancleBtn.setOnClickListener(this);
mSureBtn.setOnClickListener(this);
builder.setView(view);
mDialog = builder.create();
mDialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside);
showMySimpleDialog();
}
public void showMySimpleDialog(){
if(!mDialog.isShowing()){
mTitleTv.setText(mTextTitle);
mContentTv.setText(mTextContent);
mCancleBtn.setText(mTextCancle);
mSureBtn.setText(mTextSure);
mDialog.show();
WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
DisplayMetrics dm = new DisplayMetrics();
dm = mContext.getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
layoutParams.width = (int) (screenWidth * 0.8);
layoutParams.height = LayoutParams.WRAP_CONTENT;
mDialog.getWindow().setAttributes(layoutParams);
}
}
public void disMissMySimpleDialog(){
if(mDialog !=null && mDialog.isShowing()){
mDialog.dismiss();
}
}
public void setCancleBtnVisibility(boolean visibile){
if (visibile) {
mCancleBtn.setVisibility(View.VISIBLE);
}else {
mCancleBtn.setVisibility(View.GONE);
}
}
public void setSureBtnVisibility(boolean visibile){
if (visibile) {
mSureBtn.setVisibility(View.VISIBLE);
}else {
mSureBtn.setVisibility(View.GONE);
}
}
//确定取消监听
private OnMySimpleDialogListener mSimpleDialogistener;
public void SetOnMySimpleDialogListener(OnMySimpleDialogListener listener){
this.mSimpleDialogistener = listener;
}
public interface OnMySimpleDialogListener{
public void onMyCancle();
public void onMySure();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.dialog_btn_cancle) {
disMissMySimpleDialog();
if(mSimpleDialogistener != null ){
mSimpleDialogistener.onMyCancle();
}
}else if (v.getId() == R.id.dialog_btn_sure) {
disMissMySimpleDialog();
if(mSimpleDialogistener != null ){
mSimpleDialogistener.onMySure();
}
}
}
}
核心的就这些,具体的参照demo就可以。