当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿QQ复制昵称效果

Android仿QQ复制昵称效果

2019年05月01日  | 移动技术网IT编程  | 我要评论

创业(万创中国),用面包机做面包,盗车飞侠

本文同步自

背景:

这几天做一个复制文本的需求,突然看到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);
        });
    }
}

代码下载地址

链接: 密码:lsf7

其他相关

同事找到了一个不错的开源项目, 突然觉得这个用起来更容易,下篇来写。


 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网