zoukankan      html  css  js  c++  java
  • 点击TextView 弹出复制选项

    extends:http://www.eoeandroid.com/thread-226805-1-1.html

    package com.dotfive.chuanbang.view;
    
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.OvalShape;
    import android.text.ClipboardManager;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.PopupWindow;
    import android.widget.PopupWindow.OnDismissListener;
    
    import com.dotfive.chuanbang.R;
    import com.rockerhieu.emojicon.EmojiconTextView;
    
    public class CopyTextView extends EmojiconTextView {
        private Context mContext;
        private PopupWindow mPopupWindow;
    
        public CopyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            this.setOnLongClickListener( new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    setTextColor(Color.BLACK);
                    getPopupWindowsInstance();
                    mPopupWindow.showAsDropDown(CopyTextView.this,getWidth()/2-mPopupWindow.getWidth()/2,0);
                    return true;
                }
            });
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return super.onTouchEvent(event);
        }
        /**
         * 销毁 PopupWindow
         */
        private void dismissPopupWindowInstance(){
            if (null != mPopupWindow) {
                mPopupWindow.dismiss();
            }
        }
        /**
         * 获得 PopupWindow 实例
         */
        private void getPopupWindowsInstance(){
            if(mPopupWindow!=null){
                mPopupWindow.dismiss();
            }else{
                initPopuptWindow();
            }
        }
        /*
         * 创建PopupWindow
         */
        private void initPopuptWindow() {
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            View popupWindow = layoutInflater.inflate(R.layout.item_textview_copy_popup_window, null);
            Button btnCopy = (Button) popupWindow.findViewById(R.id.btnCopy);
            btnCopy.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTextColor(android.graphics.Color.BLACK);
                    dismissPopupWindowInstance();
                    ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboard.setText(getText());
                }
            });
            //  此处 之所以  给了 PopupWindow  一个 固定的宽度  是因为 我要让 PopupWindow 的中心位置对齐  TextView的中心位置
            //  一开始 写成了ViewGroup.LayoutParams.WRAP_CONTENT  但是  各种尝试之后 我没办法 得到 PopupWindow 的宽度 如果你能获得的话 麻烦留言 告诉我
            mPopupWindow = new PopupWindow(popupWindow, dipTopx(mContext, 50),ViewGroup.LayoutParams.WRAP_CONTENT);
            // 这行代码 很重要
            mPopupWindow.setBackgroundDrawable(getDrawable());
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    setTextColor(mContext.getResources().getColorStateList(R.color.littleGray));
                }
            });
        }
        /**
         * 生成一个 透明的背景图片
         * @return
         */
        private Drawable getDrawable(){
            ShapeDrawable bgdrawable =new ShapeDrawable(new OvalShape());
            bgdrawable.getPaint().setColor(getResources().getColor(android.R.color.transparent));
            return   bgdrawable;
        }
        /**
         * 转换成对应的px值
         * @param context
         * @param dipValue
         * @return
         */
        public static int dipTopx(Context context, float dipValue){
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int)(dipValue * scale + 0.5f);
        }
    }
  • 相关阅读:
    1.权限管理系统
    Django实战1-权限管理功能实现-01:搭建开发环境
    Django实战1-权限管理功能实现-02:项目设置
    Django实战1-权限管理功能实现-03:用户认证
    Django实战1-权限管理功能实现-04:系统入口
    Django实战1-权限管理功能实现-05:组织架构的添加
    Django实战1-权限管理功能实现-06:知识扩展-Django表单
    2.项目环境搭建
    mysql 基础
    Spring Security中 SecurityContextHolder.getContext().getAuthentication().getPrincipal()获取当前用户
  • 原文地址:https://www.cnblogs.com/niray/p/4292989.html
Copyright © 2011-2022 走看看