实现效果
创新互联-专业网站定制、快速模板网站建设、高性价比浈江网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式浈江网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖浈江地区。费用合理售后完善,10余年实体公司更值得信赖。
使用ContextMenu
(1)注册菜单
@Override
public void onCreate(Bundle savedInstanceState) {
mMoblieTextView = (TextView) findViewById(R.id.mobile_textview);
//注册上下文菜单
registerForContextMenu(mMoblieTextView);
}
(2)onCreateContextMenu添加菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
//在上下文菜单选项中添加选项内容
//add方法的参数:add(分组id,itemid, 排序, 菜单文字)
menu.add(0, 0, 0, "复制电话号码");
}
(3)点击事件响应
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == 0) {
if (null == clipboard) {
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("simple text",mMoblieTextView.getText());
// Set the clipboard's primary clip.
clipboard.setPrimaryClip(clip);
return true;
}
return super.onContextItemSelected(item);
}
2.使用OnLongClickListener 弹出 PopupWindow实现
(1)OnLongClickListener事件响应
PopupWindow PopupmenuCopy;
// 帮助窗口
TextView mPopUpBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
mMoblieTextView = (TextView) findViewById(R.id.mobile_textview);
mMoblieTextView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
popupCopy();
return false;
}
});
}
(2)创建窗口
private void popupCopy() {
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutView = layoutInflater.inflate(
R.layout.popup_copy, null);
// Creating the PopupWindow
RelativeLayout popLayout = (RelativeLayout) layoutView
.findViewById(R.id.popLayout);
scaleFontSize(popLayout);
PopupmenuCopy = new PopupWindow(mContext);
PopupmenuCopy.setContentView(layoutView);
PopupmenuCopy.setWidth(LayoutParams.WRAP_CONTENT);
PopupmenuCopy.setHeight(LayoutParams.WRAP_CONTENT);
PopupmenuCopy.setBackgroundDrawable(getResources().getDrawable(
R.drawable.left_drawer_item_bg));
PopupmenuCopy.setFocusable(true);// 点击空白处时,隐藏掉pop窗口
PopupmenuCopy.setOutsideTouchable(true);
mPopUpBtn = (TextView) layoutView.findViewById(R.id.popbtn);
// 帮助页面按钮事件响应
mPopUpBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (null == clipboard) {
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("simple text",mMoblieTextView.getText());
clipboard.setPrimaryClip(clip);
PopupmenuCopy.dismiss();
}
});
int[] location = new int[2];
mMoblieTextView.getLocationOnScreen(location);
PopupmenuCopy.showAtLocation(mMoblieTextView,Gravity.NO_GRAVITY, location[0],location[1]+mMoblieTextView.getHeight());
//控件上方
//PopupmenuCopy.showAtLocation(mMoblieTextView,Gravity.NO_GRAVITY, location[0], location[1]-PopupmenuCopy.getHeight());
}
(3)popup_copy布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
>
<TextView
android:id="@+id/popbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/copy_mobileID"
android:textSize= "@dimen/add_item_text_size"
android:background="@drawable/btn_copy_selector"
android:textColor="@color/black"
android:gravity="center"
android:layout_centerInParent="true"
/>
</RelativeLayout>
3.使用使用OnLongClickListener 弹出 QuickAction实现
QuickAction mQuickAction;
mMoblieTextView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
mQuickAction.show(v);
return false;
}
});
ActionItem copyItem = new ActionItem(0, getString(R.string.copy_mobileID));
mQuickAction = new QuickAction(mContext);
mQuickAction.addActionItem(copyItem);
//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
switch (actionId) {
case 0:
if (null == clipboard) {
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
ClipData clip = ClipData.newPlainText("simple text",mMoblieTextView.getText());
clipboard.setPrimaryClip(clip);
break;
default:
break;
}
}
});
当前名称:TextView长按复制实现方法3种方法实践总结
文章来源:https://www.cdcxhl.com/article16/pohjdg.html
成都网站建设公司_创新互联,为您提供面包屑导航、网站排名、域名注册、商城网站、用户体验、企业建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联