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

無(wú)法在 TextInputLayout 內(nèi)的 EditText 中使用 drawable

Unable to use drawable in EditText inside TextInputLayout(無(wú)法在 TextInputLayout 內(nèi)的 EditText 中使用 drawable)
本文介紹了無(wú)法在 TextInputLayout 內(nèi)的 EditText 中使用 drawable的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我最近將 Android 設(shè)計(jì)庫(kù)從 24.2.1 升級(jí)到了 25.0.0.在此之后 EditText 中的drawableX"功能不起作用.

I recently upgraded Android Design library from 24.2.1 to 25.0.0. After this the "drawableX" feature in EditText doesn't work.

編輯 01.11:我了解到,如果您使用 android:drawableStart 而不是 android:drawableLeft,則在 xml 中設(shè)置 drawable 有效.但是以編程方式設(shè)置設(shè)置drawables不起作用.我用它來(lái)制作一個(gè)清除"按鈕來(lái)清空 EditText.但是這個(gè)功能現(xiàn)在被打破了.如果這是來(lái)自 Google 的故意或錯(cuò)誤,我將不勝感激任何解決方法或知識(shí)!

EDIT 01.11: I learned that setting drawable in xml works if you use android:drawableStart instead of android:drawableLeft. But setting setting drawables programatically does not work. I use this to make a "Clear"-button to empty the EditText. But this feature is broken now. I would appreciate any work-arounds or knowledge about if this is intentional from Google or a bug!

我的可清除編輯文本代碼以前有效但現(xiàn)在無(wú)效:

My code for Clearable edit-text that worked before but doesn't now:

public class ClearableErrorTextInputEditText extends ErrorTextInputEditText implements View.OnFocusChangeListener, View.OnTouchListener, TextWatcher {

private Drawable resIcon;
private OnFocusChangeListener childFocusListener;

public ClearableErrorTextInputEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setup();
}

public ClearableErrorTextInputEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
}

public ClearableErrorTextInputEditText(Context context) {
    super(context);
    setup();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (getCompoundDrawables()[2] != null) {
            final boolean tappedClose = event.getX() > (getWidth() - getPaddingRight() - resIcon.getIntrinsicWidth());
            if (tappedClose) {
                setText("");
                return false; // true will fail on emulator running 2.1 and physical keyboard / scroll wheel
            }
        }
    }
    return false;
}

@Override
public void setOnFocusChangeListener(OnFocusChangeListener l) {
    childFocusListener = l;
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    setClearIconVisible(hasFocus && getText().length() > 0);

    if (childFocusListener!=null){
        childFocusListener.onFocusChange(v, hasFocus);
    }
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
    super.onTextChanged(s, start, before, count);
    setClearIconVisible(isFocused() && s.length() > 0);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} // not interesting

@Override
public void afterTextChanged(Editable s) {} //  // not interesting

private void setup() {
    if(isInEditMode()){
        return;
    }

    resIcon = getResources().getDrawable(R.drawable.ic_clear, null);
    resIcon.setBounds(0, 0, resIcon.getIntrinsicWidth(), resIcon.getIntrinsicHeight());

    setClearIconVisible(false);

    super.setOnTouchListener(this);
    super.setOnFocusChangeListener(this);
    addTextChangedListener(this);
}

private void setClearIconVisible(final boolean visible){
    final Drawable icon = visible ? resIcon : null;
    setCompoundDrawables(getCompoundDrawables()[0],
            getCompoundDrawables()[1], icon, getCompoundDrawables()[3]);
}

推薦答案

根據(jù) Ahmed Ashraf G 的回答,我找到了解決方案.更改以下代碼:

Based on the answer by Ahmed Ashraf G, I was able to find the solution. Changing the following code:

setCompoundDrawables(getCompoundDrawables()[0],
        getCompoundDrawables()[1], icon, getCompoundDrawables()[3]);

到:

setCompoundDrawablesRelative(getCompoundDrawablesRelative()[0],
            getCompoundDrawablesRelative()[1], icon, getCompoundDrawablesRelative()[3]);

從 StackOverflow 上的其他地方(文檔沒(méi)有提到這一點(diǎn)),xxxCompoundDrawablesXxx 和 xxxCompundDrawablesRelativeXxx 之間的區(qū)別在于相對(duì)版本考慮了 RTL 語(yǔ)言,即它們與使用 drawableStart 而不是 drawableLeft 相同.

From other places on StackOverflow (the documentation does NOT mention this) the difference between xxxCompoundDrawablesXxx and xxxCompundDrawablesRelativeXxx is that the relative versions take into account RTL-languages, ie they are the same as using drawableStart instead of drawableLeft.

因此,總而言之,Google 已經(jīng)破壞了 TextInputLayout 內(nèi) EditText 的所有不是 RTL 方法的方法.由于新方法是在 API 級(jí)別 17 中添加的,我認(rèn)為這是主要錯(cuò)誤,可能會(huì)在未來(lái)的更新中修復(fù)

So in conclusion, Google has broken all methods that are not RTL-methods for EditText inside TextInputLayout. Since the new methods are added in API level 17 I see this as major bug, that will probably be fixed in future updates

這篇關(guān)于無(wú)法在 TextInputLayout 內(nèi)的 EditText 中使用 drawable的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開(kāi)的 EditText 中的開(kāi)始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問(wèn)題)
主站蜘蛛池模板: 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 识禅_对禅的了解,从这里开始 | 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 贵州成人高考网_贵州成考网 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 | 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 西子馋火锅鸡加盟-太原市龙城酉鼎餐饮管理有限公司 | 智能家居全屋智能系统多少钱一套-小米全套价格、装修方案 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 光环国际-新三板公司_股票代码:838504 | 带锯机|木工带锯机圆木推台锯|跑车带锯机|河北茂业机械制造有限公司| | TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 贴板式电磁阀-不锈钢-气动上展式放料阀-上海弗雷西阀门有限公司 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 | 厂房出租_厂房出售_产业园区招商_工业地产 - 中工招商网 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 锂离子电池厂家-山东中信迪生电源 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 耐高温电缆厂家-远洋高温电缆| 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 不锈钢闸阀_球阀_蝶阀_止回阀_调节阀_截止阀-可拉伐阀门(上海)有限公司 | 短信营销平台_短信群发平台_106短信发送平台-河南路尚 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 |