欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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 突然觉得这个用起来更容易,下篇来写。