Android仿QQ复制昵称效果
程序员文章站
2022-05-03 11:52:19
本文同步自http://javaexception.com/archives/76 背景: 这几天做一个复制文本的需求,突然看到QQ上复制昵称跟QQ号的效果,觉得很不错,就想要模仿一波,办法比较简单粗暴,反编译QQ获取了那个.9图片,然后就是用PopWindow实现了。 解决办法: 自定义PopWi ......
本文同步自
背景:
这几天做一个复制文本的需求,突然看到qq上复制昵称跟qq号的效果,觉得很不错,就想要模仿一波,办法比较简单粗暴,反编译qq获取了那个.9图片,然后就是用popwindow实现了。
解决办法:
自定义popwindow
public class copycontentpopwindow extends popupwindow { private final view content; textview tvlabel; textview tvlabelfake; activity context; private oncopycontentlistener oncopycontentlistener; public copycontentpopwindow(activity context) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); content = inflater.inflate(r.layout.popwindow_copy_content, null); tvlabel = content.findviewbyid(r.id.tv_label); tvlabelfake = content.findviewbyid(r.id.tv_label_fake); tvlabel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (oncopycontentlistener != null) { oncopycontentlistener.copycontent(); } copycontentpopwindow.this.dismiss(); } }); this.setcontentview(content); this.setwidth(viewgroup.layoutparams.wrap_content); this.setheight(viewgroup.layoutparams.wrap_content); this.setfocusable(true); this.setoutsidetouchable(true); this.update(); colordrawable dw = new colordrawable(0000000000); this.setbackgrounddrawable(dw); this.context = context; } public void settvlabel(string text) { tvlabel.settext(text); tvlabelfake.settext(text); update(); } public void setoncopycontentlistener(oncopycontentlistener oncopycontentlistener) { this.oncopycontentlistener = oncopycontentlistener; } public void showpopupwindow(view parent) { if (!this.isshowing()) { int[] location = new int[2]; parent.getlocationonscreen(location); // this.showasdropdown(parent, 0, 0); showatlocation(parent, gravity.no_gravity, location[0], location[1]); } else { this.dismiss(); } } public interface oncopycontentlistener { void copycontent(); } }
具体使用代码:
public class mainactivity extends appcompatactivity { textview tvnickname; textview tvqqnum; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tvnickname = findviewbyid(r.id.tv_nick_name); tvqqnum = findviewbyid(r.id.tv_qq_num); tvnickname.setonclicklistener(v -> { copycontentpopwindow copycontentpopwindow = new copycontentpopwindow(mainactivity.this); copycontentpopwindow.settvlabel("复制"); copycontentpopwindow.setoncopycontentlistener(() -> { clipboardmanager cm = (clipboardmanager) getsystemservice(context.clipboard_service); clipdata clipdata = clipdata.newplaintext("label", tvnickname.gettext().tostring()); cm.setprimaryclip(clipdata); toast.maketext(mainactivity.this, "复制成功", toast.length_long).show(); }); copycontentpopwindow.showpopupwindow(tvnickname); }); tvqqnum.setonclicklistener(v -> { copycontentpopwindow copycontentpopwindow = new copycontentpopwindow(mainactivity.this); copycontentpopwindow.settvlabel("复制qq号"); copycontentpopwindow.setoncopycontentlistener(() -> { clipboardmanager cm = (clipboardmanager) getsystemservice(context.clipboard_service); clipdata clipdata = clipdata.newplaintext("label", "100001"); cm.setprimaryclip(clipdata); toast.maketext(mainactivity.this, "复制成功", toast.length_long).show(); }); copycontentpopwindow.showpopupwindow(tvqqnum); }); } }
代码下载地址
链接:https://pan.baidu.com/s/1jchjvq9i6jrffqwu3q1mtg 密码:lsf7
其他相关
同事找到了一个不错的开源项目,https://github.com/shangmingchao/popuplist 突然觉得这个用起来更容易,下篇来写。
上一篇: 喝香菜水发烧能好吗?香菜水有什么功效?
下一篇: 便秘吃猕猴桃有用吗
推荐阅读
-
Android自定义View仿QQ运动步数效果
-
Android仿QQ复制昵称效果的实现方法
-
Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
-
Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
-
Android仿微信键盘切换效果
-
Android仿qq分组管理的第三方库
-
Android仿淘宝切换商品列表布局效果的示例代码
-
android仿iphone主题效果的主菜单
-
Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)
-
Android编程实现仿优酷圆盘旋转菜单效果的方法详解【附demo源码下载】