您好,登錄后才能下訂單哦!
背景:
這幾天做一個復制文本的需求,突然看到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復制昵稱效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。