仿IOS弹出框及提示框(含Demo)

简介: 仿IOS弹出框及提示框(含Demo)

做项目经常需要用到各种各样的提示框,今天先把通用的提示框和仿IOS的弹出框写到demo里面后面会持续更新其他的。

 

1.提示框效果

image.png

2.仿IOS弹出框效果

image.png

Activity代码

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弹框

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);
  }
}

通用提示框代码

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();
      }
    }
  }
}


相关文章
|
3月前
|
语音技术 开发工具 图形学
Unity与IOS⭐一、百度语音IOS版Demo调试方法
Unity与IOS⭐一、百度语音IOS版Demo调试方法
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
70 0
|
iOS开发
iOS UIKit Dynamics Demo 学习地址列表
iOS UIKit Dynamics Demo 学习地址列表
50 0
倒计时15分钟-兼容ios手机效果demo(整理)
倒计时15分钟-兼容ios手机效果demo(整理)
|
JSON 测试技术 Android开发
基于AirTest+Python的ios自动化测试demo(微信朋友圈无限点赞)
AirTest相比Appuim有个好处就是可以对GUI图片进行捕捉和最新版本支持WebView(目前Appuim不支持iOS12的WebView进行Xpath抓取)
609 0
|
测试技术 iOS开发 Python
基于Python+appium的ios自动化测试demo(更新中)
appium环境搭建可参考以下两个链接: www.jianshu.com/p/a2b79cd8b… www.jianshu.com/p/3c04e029c…
452 0
|
程序员 API Android开发
iOS开发:简单的Toast提示框实现
博主是以iOS开发出身,那就最后一篇博文就分享一下关于iOS的内容吧。iOS开发过程中,有些时候操作App的时候,需要给用户对应的响应提示操作,使用系统自带的提示框不是每种情况都适用的。
713 0
iOS开发:简单的Toast提示框实现
|
安全 Android开发 iOS开发
iOS隐私安全:用户协议及隐私政策弹框(包含超链接属性、demo支持中英文切换)
iOS隐私安全:用户协议及隐私政策弹框(包含超链接属性、demo支持中英文切换)
1347 1
iOS隐私安全:用户协议及隐私政策弹框(包含超链接属性、demo支持中英文切换)
|
算法 iOS开发
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
546 0
iOS抽奖转盘下篇:转盘主视图的实现(内含完整Demo)
|
存储 小程序 iOS开发
iOS 小知识: 再次点击tabBar刷新界面数据(更新数据期间旋转tabbar图标)【包含完整demo】
iOS 小知识: 再次点击tabBar刷新界面数据(更新数据期间旋转tabbar图标)【包含完整demo】
538 0
iOS 小知识: 再次点击tabBar刷新界面数据(更新数据期间旋转tabbar图标)【包含完整demo】