Android中复制及粘贴功能
Posted 骑鲸鱼的企鹅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中复制及粘贴功能相关的知识,希望对你有一定的参考价值。
我们开发中,经常会用到复制粘贴的功能。算是一个小功能,也比较简单。我把项目里用的,抽离出来,附带上自定义了一个简单的菜单弹窗。
来一张gif效果图:
直接上代码,文前后有Demo
正文类:
列表内容
/**
* Created by zhenqi
*/
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
EditText editText = (EditText) findViewById(R.id.editText);
//第一个文字的长按监听
textView.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View view)
showMenu(textView);
return true;
);
//第二个文字的长按监听
textView2.setOnLongClickListener(new View.OnLongClickListener()
@Override
public boolean onLongClick(View view)
showMenu(textView2);
return true;
);
public void showMenu(final TextView textView)
//可以按照需求随意添加 中心显示的Dialog
CenterMenuDialog centerMenuDialog = new CenterMenuDialog(MainActivity.this);
//第一个选择条目
Menu copyMenu = new Menu.Builder().setCaption("复制").setMenuCommand(new MenuCommand()
@Override
public void onClick()
ClipboardManager myClipboard;
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
myClipboard.setPrimaryClip(ClipData.newPlainText("content", textView.getText()));
Toast.makeText(MainActivity.this, "内容已经复制", Toast.LENGTH_SHORT).show();
).build();
centerMenuDialog.addMenu(copyMenu);
//第二个选择条目
Menu themeMenu = new Menu.Builder().setCaption("取消").setMenuCommand(new MenuCommand()
@Override
public void onClick()
Toast.makeText(MainActivity.this, "已经取消", Toast.LENGTH_SHORT).show();
).build();
centerMenuDialog.addMenu(themeMenu);
//显示Dialog
centerMenuDialog.show();
弹窗类:
/**
* Created by zhenqi
* 中心弹窗类
*/
public class CenterMenuDialog
AlertDialog.Builder builder;
List<Menu> menuList=new ArrayList<>();
public void addMenu(Menu menu)
menuList.add(menu);
public CenterMenuDialog(Context context)
builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
public void show()
String arr[]=new String[menuList.size()];
for (int i = 0; i < menuList.size(); i++)
arr[i]=menuList.get(i).caption;
builder.setItems(arr, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialogInterface, int i)
Menu menu=menuList.get(i);
if(menu.menuCommand!=null)
menu.menuCommand.onClick();
);
builder.create().show();
菜单类:
/**
* Created by zhenqi
* 自定义菜单类
*/
public class Menu
public int resourceId;
public String caption;
public MenuCommand menuCommand;
public static class Builder
int resourceId;
String caption;
MenuCommand menuCommand;
public Builder setResourceId(int resourceId)
this.resourceId=resourceId;
return this;
public Builder setCaption(String caption)
this.caption=caption;
return this;
public Builder setMenuCommand(MenuCommand menuCommand)
this.menuCommand=menuCommand;
return this;
public Menu build()
Menu menu=new Menu();
menu.resourceId=resourceId;
menu.menuCommand=menuCommand;
menu.caption=caption;
return menu;
接口
/**
* Created by zhenqi
*/
public interface MenuCommand
void onClick();
以上是关于Android中复制及粘贴功能的主要内容,如果未能解决你的问题,请参考以下文章