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);
        }
    }
  • 相关阅读:
    (转)swc使用
    (转)AS3中的反射相关
    AS获取url参数
    (转)深入理解Flash Player的安全域(Security Domains)
    (转)html<embed>标签和url向Flash传flashvars值
    (转)ApplicationDomain
    灰度发布
    (转)flex中使用swc实现更好的界面代码分离
    (转)深入理解Flash Player的应用程序域(Application Domains)
    嵌入式系统可执行文件格式
  • 原文地址:https://www.cnblogs.com/niray/p/4292989.html
Copyright © 2011-2022 走看看