pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

EditText:禁用文本選擇處理程序單擊事件上的粘貼

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
本文介紹了EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的目標是擁有一個沒有花哨功能的 EditText,只是用于更輕松地移動光標的文本選擇處理程序——因此沒有上下文菜單或彈出窗口.

根據 類,并相應地阻止所有事件.

有關堅韌不拔的細節,請繼續閱讀.

解決辦法在于防止PASTE/REPLACE菜單出現在show() 方法的(未記錄)android.widget.Editor 類.在菜單出現之前,會檢查 if (!canPaste && !canSuggest) return;.作為設置這些變量的基礎的兩個方法都在 EditText 類中:

  • isSuggestionsEnabled() 是 public,因此可能會被覆蓋.
  • canPaste() 不是,因此必須被 在派生類中引入同名函數.

因此,將這些更新合并到一個類中,該類也具有 setCustomSelectionActionModeCallback 和 禁用長按,這里是防止所有編輯的完整類(但仍然顯示文本選擇處理程序) 用于控制光標:

包 com.cjbs.widgets;導入android.content.Context;導入android.util.AttributeSet;導入android.view.ActionMode;導入 android.view.Menu;導入android.view.MenuItem;導入 android.widget.EditText;/*** 這是 EditText 上的薄薄的一層,刪除了復制/粘貼/拼寫檢查.*/公共類 NoMenuEditText 擴展 EditText{私有最終上下文上下文;/** 這是基本 TextView 類的同名方法的替換方法.這* 在隱藏類android.widget.Editor中使用方法來判斷PASTE/REPLACE是否彈出* 從文本插入句柄觸發時出現.返回 false 強制此窗口*永遠不會出現.* @return 假*/布爾值 canPaste(){返回假;}/** 這是基本 TextView 類的同名方法的替換方法.這種方法* 用于隱藏類android.widget.Editor 判斷PASTE/REPLACE 是否彈出* 從文本插入句柄觸發時出現.返回 false 強制此窗口*永遠不會出現.* @return 假*/@覆蓋公共布爾 isSuggestionsEnabled(){返回假;}公共 NoMenuEditText(上下文上下文){超級(上下文);this.context = 上下文;在里面();}public NoMenuEditText(上下文上下文,AttributeSet attrs){超級(上下文,屬性);this.context = 上下文;在里面();}public NoMenuEditText(上下文上下文,AttributeSet attrs,int defStyle){超級(上下文,屬性,defStyle);this.context = 上下文;在里面();}私人無效初始化(){this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());this.setLongClickable(false);}/*** 防止出現操作欄(帶有剪切、復制、粘貼等的頂部水平欄)* 通過攔截將導致它被創建的回調,并返回 false.*/私有類 ActionModeCallbackInterceptor 實現 ActionMode.Callback{private final String TAG = NoMenuEditText.class.getSimpleName();public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false;}public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false;}public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false;}公共無效 onDestroyActionMode(ActionMode 模式) {}}}

我已經在 Android v4.4.2 和 v4.4.3 中對此進行了測試.

My goal is to have an EditText that has no fancy features, just the Text Selection Handler for moving the cursor more easily -- so no context menus or pop-ups.

I've disabled the appearance of the text editing function actionbar (copy/Paste etc.) by consuming the ActionMode Callback event, as per this solution.

The middle Middle Text Select Handle (see image below) still appears when text exists in the field and a click occurs within the text. Great! I want to keep this behaviour. What I DON'T want is the "PASTE" menu to appear when the Text Select Handle itself is clicked.

I have also disabled long-click input for the EditText by setting android:longClickable="false" in the styles XML. Disabling the long click prevents the "Paste/Replace" menu from appearing when the mouse is clicked and held (i.e. long touch), however when the mouse is clicked (single touch) within the text, the text selection handle appears, and when the text selection handle itself is clicked, then the "paste" menu option appears (when there's text in the clipboard). This is what I'm trying to prevent.

From what I can see from the source, the ActionPopupWindow is what pops up with the PASTE/REPLACE options. ActionPopupWindow is a protected variable (mActionPopupWindow) in the private abstract class HandleView within public class android.widget.Editor...

Short of disabling the clipboard service or editing the Android Source code, is there a way that I can prevent this from showing? I tried to define a new style for android:textSelectHandleWindowStyle, and set android:visibility to gone, but it didn't work (app froze for a while when it would otherwise have shown).

解決方案

Solution: Override isSuggestionsEnabled and canPaste in EditText.

For the quick solution, copy the class below - this class overrides the EditText class, and blocks all events accordingly.

For the gritty details, keep reading.

The solution lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

  • isSuggestionsEnabled() is public, and may thus be overridden.
  • canPaste() is not, and thus must be hidden by introducing a function of the same name in the derived class.

So incorporating these updates into a class that also has the setCustomSelectionActionModeCallback, and the disabled long-click, here is the full class to prevent all editing (but still display the text selection handler) for controlling the cursor:

package com.cjbs.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


/**
 *  This is a thin veneer over EditText, with copy/paste/spell-check removed.
 */
public class NoMenuEditText extends EditText
{
    private final Context context;

    /** This is a replacement method for the base TextView class' method of the same name. This 
     * method is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
     * appears when triggered from the text insertion handle. Returning false forces this window
     * to never appear.
     * @return false
     */
    boolean canPaste()
    {
       return false;
    }

    /** This is a replacement method for the base TextView class' method of the same name. This method
     * is used in hidden class android.widget.Editor to determine whether the PASTE/REPLACE popup
     * appears when triggered from the text insertion handle. Returning false forces this window
     * to never appear.
     * @return false
     */
    @Override
    public boolean isSuggestionsEnabled()
    {
        return false;
    }

    public NoMenuEditText(Context context)
    {
        super(context);
        this.context = context;
        init();
    }

    public NoMenuEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        init();
    }

    public NoMenuEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init()
    {
        this.setCustomSelectionActionModeCallback(new ActionModeCallbackInterceptor());
        this.setLongClickable(false);
    }


    /**
     * Prevents the action bar (top horizontal bar with cut, copy, paste, etc.) from appearing
     * by intercepting the callback that would cause it to be created, and returning false.
     */
    private class ActionModeCallbackInterceptor implements ActionMode.Callback
    {
        private final String TAG = NoMenuEditText.class.getSimpleName();

        public boolean onCreateActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
        public void onDestroyActionMode(ActionMode mode) {}
    }
} 

I've tested this in Android v4.4.2 and v4.4.3.

這篇關于EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
Contact Bubble EditText(聯系氣泡編輯文本)
主站蜘蛛池模板: 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂| 吨袋包装机|吨包秤|吨包机|集装袋包装机-烟台华恩科技 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 自动焊锡机_点胶机_螺丝机-锐驰机器人 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 便携式谷丙转氨酶检测仪|华图生物科技百科 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 搜木网 - 木业全产业链交易平台,免费搜货、低价买货! | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 厂房出租_厂房出售_产业园区招商_工业地产 - 中工招商网 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 长城人品牌官网| 建筑资质代办_工程施工资质办理_资质代办公司_北京众聚企服 | 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 数控走心机-双主轴走心机厂家-南京建克 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 窖井盖锯圆机_锯圆机金刚石锯片-无锡茂达金刚石有限公司 | 电镀电源整流器_高频电解电源_单脉双脉冲电源 - 东阳市旭东电子科技 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 |