Android textView复制,popupwindow显示在文字上方
2016-01-26 16:24
555 查看
/** 点击复制 的按钮 */ private TextView textView_copy; /** 复制文字跳出的popupwindow */ private PopupWindow popupWindow; /** 设置popup的显示位置 */ private int[] location; /**popup的宽*/ private int popupWidth; /**popup的高*/ private int popupHeight; /** 设置复制的popupwindow */ private void setPopupWindow() { textView_copy = new TextView(context); textView_copy.setBackgroundResource(R.drawable.popover); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); textView_copy.setLayoutParams(params); textView_copy.setGravity(Gravity.CENTER); textView_copy.setPadding(0, 0, 0, 20); textView_copy.setText("复制"); textView_copy.setTextSize(14); textView_copy.setTextColor(Color.WHITE); popupWindow = new PopupWindow(textView_copy, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); location = new int[2]; textView_copy.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); popupWindow.setFocusable(true); popupWidth = textView_copy.getMeasuredWidth(); popupHeight = textView_copy.getMeasuredHeight(); } textView_copy.setOnKeyListener(new OnKeyListener() {//设置返回键的事件处理,让popuwindow消失 @Override public boolean onKey(View v, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } break; default: break; } return false; } }); tv_content.setOnLongClickListener(new OnLongClickListener() {//长按textView的监听,tv_content为点击的textview @Override public boolean onLongClick(View v) { v.getLocationOnScreen(location);//当前的localtion在整个屏幕的坐标位置 if (!popupWindow.isShowing()) { popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,// 设置popup在控件的上方中间偏移量的位置出现 (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight); textView_copy.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.setPrimaryClip(ClipData.newPlainText(null, tv_content.getText().toString())); } }); } return false; } });
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories