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

多個 EditTexts 的焦點問題

Focus issue with multiple EditTexts(多個 EditTexts 的焦點問題)
本文介紹了多個 EditTexts 的焦點問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個包含兩個 EditText 的活動.我在第二個 EditText 字段上調用 ??requestFocus 因為默認情況下焦點轉到第一個.焦點似乎在第二個字段中(第二個獲得突出顯示的邊框),但是如果我們嘗試使用硬件鍵盤輸入任何字符,文本會出現在第一個 EditText 控件中.任何想法為什么會發生?

I have an activity with two EditTexts. I am calling the requestFocus on the second EditText field since by default the focus goes to the first one. The focus appears to be in the second field (the second one gets the highlighted border), but if we try to enter any characters using the hardware keyboard the text appears in the first EditText control. Any ideas why it would be happening?

推薦答案

很難說這是否是你的問題,但也不是不可能.

It's hard to tell whether this was your problem, but it's not unlikely.

TL;DR:永遠不要從 onFocusChanged() 調用中調用像 requestFocus() 這樣的焦點更改方法.

TL;DR: Never call focus-changing methods like requestFocus() from inside a onFocusChanged() call.

問題在于 ViewGroup.requestChildFocus(),其中包含:

The issue lies in ViewGroup.requestChildFocus(), which contains this:

// We had a previous notion of who had focus. Clear it.
if (mFocused != child) {
    if (mFocused != null) {
        mFocused.unFocus();
    }

    mFocused = child;
}

在私有字段 mFocused 中,ViewGroup 存儲當前具有焦點的子視圖(如果有).

Inside the private field mFocused a ViewGroup stores the child view that currently has focus, if any.

假設您有一個 ViewGroup VG,其中包含三個可聚焦視圖(例如 EditTexts)ABC.

Say you have a ViewGroup VG that contains three focusable views (e.g. EditTexts) A, B, and C.

您已向 A 添加了一個 OnFocusChangeListener (可能不是直接的,而是嵌套在其中的某處)在 B.requestFocus() 時調用 B.requestFocus()code>A失去焦點.

You have added an OnFocusChangeListener to A that (maybe not directly, but somewhere nested inside) calls B.requestFocus() when A loses focus.

現在假設A有焦點,用戶點擊C,導致A丟失,C 獲得焦點.因為 VG.mFocused 當前是 A,所以 VG.requestChildFocus(C, C) 的上面部分然后翻譯成這樣:

Now imagine that A has focus, and the user taps on C, causing A to lose and C to gain focus. Because VG.mFocused is currently A, the above part of VG.requestChildFocus(C, C) then translates to this:

if (A != C) {
    if (A != null) {
        A.unFocus();          // <-- (1)
    }

    mFocused = C;             // <-- (3)
}

A.unFocus() 在這里做了兩件重要的事情:

A.unFocus() does two important things here:

  1. 它將 A 標記為不再具有焦點.

  1. It marks A as not having focus anymore.

它會調用你的焦點變化監聽器.

It calls your focus change listener.

在該偵聽器中,您現在調用 B.requestFocus().這會導致 B 被標記為具有焦點,然后調用 VG.requestChildFocus(B, B).因為我們仍然深入我用 (1) 標記的調用,所以 mFocused 的值仍然是 A,因此這個內部調用如下所示:

In that listener, you now call B.requestFocus(). This causes B to be marked as having focus, and then calls VG.requestChildFocus(B, B). Because we're still deep inside the call I've marked with (1), the value of mFocused is still A, and thus this inner call looks like this:

if (A != B) {
    if (A != null) {
        A.unFocus();
    }

    mFocused = B;             // <-- (2)
}

這一次,對 A.unFocus() 的調用沒有做任何事情,因為 A 已經被標記為未聚焦(否則我們將進行無限遞歸這里).此外,沒有任何事情將 C 標記為未聚焦,這是 實際上 現在具有焦點的視圖.

This time, the call to A.unFocus() doesn't do anything, because A is already marked as unfocused (otherwise we'd have an infinite recursion here). Also, nothing happens that marks C as unfocused, which is the view that actually has focus right now.

現在是 (2),它將 mFocused 設置為 B.經過更多的工作,我們終于從 (1) 處的調用返回,因此在 (3) 處,現在設置了 mFocused 的值到 C覆蓋之前的更改.

Now comes (2), which sets mFocused to B. After some more stuff, we finally return from the call at (1), and thus at (3) the value of mFocused is now set to C, overwriting the previous change.

所以現在我們最終進入了一個不一致的狀態.BC 都認為他們有焦點,VG 認為 C 是有焦點的孩子.

So now we end up with an incosistent state. B and C both think they have focus, VG considers C to be the focused child.

特別是,按鍵以 C 結束,并且用戶不可能將焦點切換回 B,因為 >B 認為它已經有了焦點,因此不對焦點請求做任何事情;最重要的是,它調用VG.requestChildFocus.

In particular, keypresses end up in C, and it is impossible for the user to switch focus back to B, because B thinks it already has focus and thus doesn't do anything on focus requests; most importantly, it does not call VG.requestChildFocus.

推論:您也不應該在 OnFocusChanged 處理程序中依賴 hasFocus() 調用的結果,因為在該調用中焦點信息不一致.

Corollary: You also shouldn't rely on results from hasFocus() calls while inside an OnFocusChanged handler, because the focus information is inconsistent while inside that call.

這篇關于多個 EditTexts 的焦點問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
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 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 石磨面粉机|石磨面粉机械|石磨面粉机组|石磨面粉成套设备-河南成立粮油机械有限公司 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 酒糟烘干机-豆渣烘干机-薯渣烘干机-糟渣烘干设备厂家-焦作市真节能环保设备科技有限公司 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 云南丰泰挖掘机修理厂-挖掘机维修,翻新,再制造的大型企业-云南丰泰工程机械维修有限公司 | 百度网站优化,关键词排名,SEO优化-搜索引擎营销推广 | 不锈钢电动球阀_气动高压闸阀_旋塞疏水调节阀_全立阀门-来自温州工业阀门巨头企业 | 宠物店加盟_宠物连锁店_开宠物店-【派多格宠物】 | 中式装修设计_室内中式装修_【云臻轩】中式设计机构 | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | LINK FASHION 童装·青少年装展 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 盘装氧量分析仪-防爆壁挂氧化锆分析仪-安徽吉帆仪表有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 礼堂椅厂家|佛山市艺典家具有限公司 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 杭州ROHS检测仪-XRF测试仪价格-百科| 纯化水设备-EDI-制药-实验室-二级反渗透-高纯水|超纯水设备 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 深圳办公室装修-写字楼装修设计-深圳标榜装饰公司 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 数控走心机-走心机价格-双主轴走心机-宝宇百科| 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 |