webview长按复制_android长按菜单

webview长按复制_android长按菜单TextView长按复制实现方法3种方法实践总结发布时间:2020-09-0514:32:58来源:51CTO阅读:1003作者:huangwenwenlili实现效果使用ContextMenu(1)注册菜单@OverridepublicvoidonCreate(BundlesavedInstanceState){mMoblieTextView=(TextView)findVie…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

TextView长按复制实现方法 3种方法实践总结

发布时间:2020-09-05 14:32:58

来源:51CTO

阅读:1003

作者:huangwenwenlili

实现效果

5ee49755420fd4378322000063ea8fd1.png

b5c3890273588f22ff6bc48ee24e9cad.png

80de402cbc09d906860507281d7c9262.png使用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布局文件

android:id=”@+id/popLayout”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:background=”@color/transparent”

>

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”

/>

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;

}

}

});

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/187833.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号