高仿IOS的Android弹出框
程序员文章站
2023-12-18 19:46:58
先看一下效果图,不过这是网上的图片。
效果不错,就借此拿来与大伙分享分享。
github源码地址:https://github.com/saiwu-bigk...
先看一下效果图,不过这是网上的图片。
效果不错,就借此拿来与大伙分享分享。
github源码地址:https://github.com/saiwu-bigkoo/android-alertview.
1.怎么用:添加依赖。
compile 'com.bigkoo:alertview:1.0.3'
2.实例demo(大家可以根据需要来选择自己需要的框框)。
package com.example.my.androidalertview; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.keyevent; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.inputmethod.inputmethodmanager; import android.widget.edittext; import android.widget.toast; import com.bigkoo.alertview.alertview; import com.bigkoo.alertview.ondismisslistener; import com.bigkoo.alertview.onitemclicklistener; /** * 精仿iosalertviewcontroller控件demo */ public class mainactivity extends activity implements onitemclicklistener, ondismisslistener { private alertview malertview;//避免创建重复view,先创建view,然后需要的时候show出来,推荐这个做法 private alertview malertviewext;//窗口拓展例子 private edittext etname;//拓展view内容 private inputmethodmanager imm; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imm = (inputmethodmanager) getsystemservice(context.input_method_service); malertview = new alertview("标题", "内容", "取消", new string[]{"确定"}, null, this, alertview.style.alert, this).setcancelable(true).setondismisslistener(this); //拓展窗口 malertviewext = new alertview("提示", "请完善你的个人资料!", "取消", null, new string[]{"完成"}, this, alertview.style.alert, this); viewgroup extview = (viewgroup) layoutinflater.from(this).inflate(r.layout.alertext_form, null); etname = (edittext) extview.findviewbyid(r.id.etname); etname.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view view, boolean focus) { //输入框出来则往上移动 boolean isopen = imm.isactive(); malertviewext.setmarginbottom(isopen && focus ? 120 : 0); system.out.println(isopen); } }); malertviewext.addextview(extview); } public void alertshow1(view view) { malertview.show(); } public void alertshow2(view view) { new alertview("标题", "内容", null, new string[]{"确定"}, null, this, alertview.style.alert, this).show(); } public void alertshow3(view view) { new alertview(null, null, null, new string[]{"高亮按钮1", "高亮按钮2", "高亮按钮3"}, new string[]{"其他按钮1", "其他按钮2", "其他按钮3", "其他按钮4", "其他按钮5", "其他按钮6", "其他按钮7", "其他按钮8", "其他按钮9", "其他按钮10", "其他按钮11", "其他按钮12"}, this, alertview.style.alert, this).show(); } public void alertshow4(view view) { new alertview("标题", null, "取消", new string[]{"高亮按钮1"}, new string[]{"其他按钮1", "其他按钮2", "其他按钮3"}, this, alertview.style.actionsheet, this).show(); } public void alertshow5(view view) { new alertview("标题", "内容", "取消", null, null, this, alertview.style.actionsheet, this).setcancelable(true).show(); } public void alertshow6(view view) { new alertview("上传头像", null, "取消", null, new string[]{"拍照", "从相册中选择"}, this, alertview.style.actionsheet, this).show(); } public void alertshowext(view view) { malertviewext.show(); } private void closekeyboard() { //关闭软键盘 imm.hidesoftinputfromwindow(etname.getwindowtoken(), 0); //恢复位置 malertviewext.setmarginbottom(0); } @override public void onitemclick(object o, int position) { closekeyboard(); //判断是否是拓展窗口view,而且点击的是非取消按钮 if (o == malertviewext && position != alertview.cancelposition) { string name = etname.gettext().tostring(); if (name.isempty()) { toast.maketext(this, "啥都没填呢", toast.length_short).show(); } else { toast.maketext(this, "hello," + name, toast.length_short).show(); } return; } toast.maketext(this, "点击了第" + position + "个", toast.length_short).show(); } @override public void ondismiss(object o) { closekeyboard(); toast.maketext(this, "消失了", toast.length_short).show(); } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back && event.getrepeatcount() == 0) { if (malertview != null && malertview.isshowing()) { malertview.dismiss(); return false; } } return super.onkeydown(keycode, event); } }
布局文件:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin"> <button android:text="hey,alert !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow1"/> <button android:text="hey,alert !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow2"/> <button android:text="hey,alert !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow3"/> <button android:text="hey,actionsheet !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow4"/> <button android:text="hey,actionsheet !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow5"/> <button android:text="hey,actionsheet !!!click here~~~" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshow6"/> <button android:text="窗口拓展点这里" android:layout_width="match_parent" android:layout_margintop="5dp" android:layout_height="50dp" android:onclick="alertshowext"/> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。