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)題)
主站蜘蛛池模板: 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 暴风影音| 地磅-地秤-江阴/无锡地磅-江阴天亿计量设备有限公司_ | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 短信通106短信接口验证码接口群发平台_国际短信接口验证码接口群发平台-速度网络有限公司 | 辐射色度计-字符亮度测试-反射式膜厚仪-苏州瑞格谱光电科技有限公司 | 北京普辉律师事务所官网_北京律师24小时免费咨询|法律咨询 | 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 济南宣传册设计-画册设计_济南莫都品牌设计公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 立式矫直机_卧式矫直机-无锡金矫机械制造有限公司 | 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 塑胶跑道施工-硅pu篮球场施工-塑胶网球场建造-丙烯酸球场材料厂家-奥茵 | 金属软管_不锈钢金属软管_巩义市润达管道设备制造有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 天津仓储物流-天津电商云仓-天津云仓一件代发-博程云仓官网 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 27PR跨境电商导航 | 专注外贸跨境电商 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 烘干设备-热泵烘干机_广东雄贵能源设备有限公司| 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 流量卡中心-流量卡套餐查询系统_移动电信联通流量卡套餐大全 | 不锈钢水箱厂家,不锈钢保温水箱-山东桑特供水设备 | 联系我们老街华纳娱乐公司官网19989979996(客服) | 自动售货机_无人售货机_专业的自动售货机运营商_免费投放售货机-广州富宏主官网 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 |