这几天做一个复制文本的需求,突然看到QQ上复制昵称跟QQ号的效果,觉得很不错,就想要模仿一波,办法比较简单粗暴,反编译QQ获取了那个.9图片,然后就是用PopWindow实现了,需要的朋友可以参考下
背景:
这几天做一个复制文本的需求,突然看到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仿QQ复制昵称效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
沃梦达教程
本文标题为:Android仿QQ复制昵称效果
猜你喜欢
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android studio实现动态背景页面 2023-05-23
- iOS 对当前webView进行截屏的方法 2023-03-01
- Flutter实现底部和顶部导航栏 2022-08-31
- Android实现监听音量的变化 2023-03-30
- Android实现轮询的三种方式 2023-02-17
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02