久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ListView 內的可聚焦 EditText

Focusable EditText inside ListView(ListView 內的可聚焦 EditText)
本文介紹了ListView 內的可聚焦 EditText的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

到目前為止,我已經在這方面花費了大約 6 個小時,并且遇到的只是障礙.一般的前提是 ListView 中有一些行(無論它是由適配器生成的,還是作為標題視圖添加的)包含一個 EditText 小部件和一個 按鈕.我想要做的就是能夠使用慢跑球/箭頭,將選擇器導航到像正常一樣的單個項目,但是當我到達特定行時——即使我必須明確標識該行——具有焦點孩子,我希望那個孩子獲得焦點,而不是用選擇器指示位置.

I've spent about 6 hours on this so far, and been hitting nothing but roadblocks. The general premise is that there is some row in a ListView (whether it's generated by the adapter, or added as a header view) that contains an EditText widget and a Button. All I want to do is be able to use the jogball/arrows, to navigate the selector to individual items like normal, but when I get to a particular row -- even if I have to explicitly identify the row -- that has a focusable child, I want that child to take focus instead of indicating the position with the selector.

我嘗試了很多可能性,但到目前為止都沒有運氣.

I've tried many possibilities, and have so far had no luck.

布局:

<ListView
    android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    />

標題視圖:

EditText view = new EditText(this);
listView.addHeaderView(view, null, true);

假設適配器中還有其他項目,使用箭頭鍵將在列表中向上/向下移動選擇,如預期的那樣;但是當到達標題行時,它也與選擇器一起顯示,并且無法使用慢跑球將焦點放在 EditText 中.注意:點擊 EditText 會將其聚焦在該點上,但這依賴于觸摸屏,這不是必需的.

Assuming there are other items in the adapter, using the arrow keys will move the selection up/down in the list, as expected; but when getting to the header row, it is also displayed with the selector, and no way to focus into the EditText using the jogball. Note: tapping on the EditText will focus it at that point, however that relies on a touchscreen, which should not be a requirement.

ListView 在這方面顯然有兩種模式:
1. setItemsCanFocus(true):選擇器從不顯示,但EditText可以在使用箭頭時獲得焦點.焦點搜索算法難以預測,并且沒有視覺反饋(在任何行:是否有可聚焦的子項)選擇了哪個項目,這兩者都可以給用戶帶來意想不到的體驗.
2. setItemsCanFocus(false):選擇器總是在非觸摸模式下繪制,EditText永遠無法獲得焦點——即使你點擊它.

ListView apparently has two modes in this regard:
1. setItemsCanFocus(true): selector is never displayed, but the EditText can get focus when using the arrows. Focus search algorithm is hard to predict, and no visual feedback (on any rows: having focusable children or not) on which item is selected, both of which can give the user an unexpected experience.
2. setItemsCanFocus(false): selector is always drawn in non-touch-mode, and EditText can never get focus -- even if you tap on it.

更糟糕的是,調用 editTextView.requestFocus() 返回 true,但實際上并沒有給 EditText 焦點.

To make matters worse, calling editTextView.requestFocus() returns true, but in fact does not give the EditText focus.

我所設想的基本上是 1 和 1 的混合體.2,如果 all 項目是否可聚焦,而不是列表設置,我想為列表中的 single 項目設置可聚焦性,以便選擇器從為不可聚焦的項選擇整行,并遍歷包含可聚焦子項的焦點樹.

What I'm envisioning is basically a hybrid of 1 & 2, where rather than the list setting if all items are focusable or not, I want to set focusability for a single item in the list, so that the selector seamlessly transitions from selecting the entire row for non-focusable items, and traversing the focus tree for items that contain focusable children.

有接受者嗎?

推薦答案

對不起,回答了我自己的問題.它可能不是最正確或最優(yōu)雅的解決方案,但它對我有用,并提供了非??煽康挠脩趔w驗.我查看了 ListView 的代碼以了解為什么這兩種行為如此不同,并從 ListView.java 中發(fā)現了這一點:

Sorry, answered my own question. It may not be the most correct or most elegant solution, but it works for me, and gives a pretty solid user experience. I looked into the code for ListView to see why the two behaviors are so different, and came across this from ListView.java:

    public void setItemsCanFocus(boolean itemsCanFocus) {
        mItemsCanFocus = itemsCanFocus;
        if (!itemsCanFocus) {
            setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        }
    }

所以,當調用 setItemsCanFocus(false) 時,它也設置了后代的可聚焦性,使得沒有孩子可以得到焦點.這就解釋了為什么我不能只在 ListView 的 OnItemSelectedListener 中切換 mItemsCanFocus - 因為 ListView 會阻止所有子級的焦點.

So, when calling setItemsCanFocus(false), it's also setting descendant focusability such that no child can get focus. This explains why I couldn't just toggle mItemsCanFocus in the ListView's OnItemSelectedListener -- because the ListView was then blocking focus to all children.

我現在擁有的:

<ListView
    android:id="@android:id/list" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:descendantFocusability="beforeDescendants"
    />

我使用 beforeDescendants 是因為只有當 ListView 本身(不是子項)具有焦點時才會繪制選擇器,因此默認行為需要是 ListView 先獲得焦點并繪制選擇器.

I use beforeDescendants because the selector will only be drawn when the ListView itself (not a child) has focus, so the default behavior needs to be that the ListView takes focus first and draws selectors.

然后在 OnItemSelectedListener 中,因為我知道要覆蓋選擇器的標題視圖(需要更多的工作來動態(tài)確定任何給定位置是否包含可聚焦視圖),我可以更改后代的可聚焦性,并將焦點設置在 EditText.當我離開該標題時,再次將其改回.

Then in the OnItemSelectedListener, since I know which header view I want to override the selector (would take more work to dynamically determine if any given position contains a focusable view), I can change descendant focusability, and set focus on the EditText. And when I navigate out of that header, change it back it again.

public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
    if (position == 1)
    {
        // listView.setItemsCanFocus(true);

        // Use afterDescendants, because I don't want the ListView to steal focus
        listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        myEditText.requestFocus();
    }
    else
    {
        if (!listView.isFocused())
        {
            // listView.setItemsCanFocus(false);

            // Use beforeDescendants so that the EditText doesn't re-take focus
            listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
            listView.requestFocus();
        }
    }
}

public void onNothingSelected(AdapterView<?> listView)
{
    // This happens when you start scrolling, so we need to prevent it from staying
    // in the afterDescendants mode if the EditText was focused 
    listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}

注意注釋掉的 setItemsCanFocus 調用.通過這些調用,我得到了正確的行為,但是 setItemsCanFocus(false) 導致焦點從 EditText 跳轉到 ListView 之外的另一個小部件,返回到 ListView 并在下一個選定的對象上顯示選擇器項目,而跳躍的焦點讓人分心.刪除 ItemsCanFocus 更改,只需切換后代可聚焦性就可以讓我獲得所需的行為.所有項目都像往常一樣繪制選擇器,但是當到達帶有 EditText 的行時,它會專注于文本字段.然后,當繼續(xù)退出該 EditText 時,它又開始繪制選擇器.

Note the commented-out setItemsCanFocus calls. With those calls, I got the correct behavior, but setItemsCanFocus(false) caused focus to jump from the EditText, to another widget outside of the ListView, back to the ListView and displayed the selector on the next selected item, and that jumping focus was distracting. Removing the ItemsCanFocus change, and just toggling descendant focusability got me the desired behavior. All items draw the selector as normal, but when getting to the row with the EditText, it focused on the text field instead. Then when continuing out of that EditText, it started drawing the selector again.

這篇關于ListView 內的可聚焦 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
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 中的數值?)
主站蜘蛛池模板: 久久99国产精品 | 中文字幕免费 | 97超碰人人| 成人在线中文字幕 | 免费视频一区二区 | 欧美精品在线播放 | 香蕉91| 国产一二三区在线 | 国产一区二区三区视频 | 欧美一级免费看 | 国产精品视频播放 | 91亚洲精品国偷拍自产在线观看 | 国产韩国精品一区二区三区 | 国产精品区二区三区日本 | 国产精品乱码一区二区三区 | 成人三级视频在线观看 | 久久久久国产精品一区二区 | 成人国产在线观看 | 日韩一区二区久久 | 国产线视频精品免费观看视频 | 黄片毛片| 日韩毛片免费看 | 超碰网址 | 一级毛片大全免费播放 | 成人免费在线播放 | 日韩精品一区二区三区在线播放 | 亚洲精品日韩欧美 | 色女人天堂 | 亚洲永久字幕 | 99久久99热这里只有精品 | 亚洲欧美中文日韩在线v日本 | 福利网站在线观看 | 毛片网站在线观看视频 | 精品免费国产 | 在线观看视频91 | 久热国产精品视频 | 成人精品一区二区三区中文字幕 | 97国产精品视频人人做人人爱 | 日韩精品视频一区二区三区 | www.日韩在线 | 天天爱天天操 |