2016-08-16 252 views
0

背景如何解決Activity中焦點搶奪問題?

開發工具:安卓2.1.3工作室

設備:Android操作系統版本。 4.4.2

我有一個有多個視圖的活動。我想根據用戶先前的操作以編程方式關注某個視圖(本例中爲editText1)。所以我爲此僱用了View.requestFocus()。在此之前,我已經設置了一個editText1focusablefocusableInThouchModetrue XML設計文件:

<EditText 
     android:id="@+id/editText1" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight=".15" 
     android:inputType="numberDecimal" 
     android:maxLength="3" 
     android:text="1" 
     android:focusableInTouchMode="true" 
     android:focusable="true" 
     android:enabled="true" /> 

理想的情況是:如果用戶當前操作前檢查一定myCheckBox,將焦點移到dditText1,如果別人,回報。

if(myCheckBox.isChecked()){ 
    editText1.selectAll(); 
    if(adet.requestFocusFromTouch()) { 
     Log.i(General.LOG_TAG, "editText1 has focus"); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT); 
    } 
    return; 
} 

但實際上,其他一些查看搶奪集中立即從editText1。我甚至可以看到editText1得到了重點,並在其中選擇了一段文字。另外,我可以在日誌中看到editText1獲得焦點。

我的活動包含一個LinearLayout和所有其他視圖(CheckBox,EditText,ListView等)都在裏面。此外,我已經超過editText1

問題

我如何防止越來越/在我的情況拼搶的重點不是我editText其他視圖其他視圖設置focusablefocusableInThouchModefalse

對於我在這裏要做的事情,有沒有其他方法?

+0

以編程方式在視圖上嘗試使用bringToFront。看看它是否有幫助。它在我的自定義arrayadapter佈局中爲我工作,whoch擁有多個Textviews和Buttons。 – VikingPingvin

+0

@VikingPingvin,我試過'bringToFront',結果是一樣的。 – raidensan

回答

0

您可以嘗試爲不應該關注的組件設置android:focusable="false"。不知道我的問題是否正確。

+0

我已經做到了,沒有運氣 – raidensan