2010-12-21 45 views
3

我創建了一個簡單的優先級,顯示了AutoCompleteTextView控制時集中精力,它顯示正常,但是當我專注於AutoCompleteTextView並開始鍵入它帶來了鍵盤,但隨即失去焦點的控制。定製的Android偏好型失去打字

任何想法,爲什麼這失去了重點?

這是我做的創建視圖。膨脹的佈局只是一個基本的線性佈局,其中包含標題文本視圖。

我可以將其更改爲對話而不是偏愛我猜,但它會是平滑的,如果它可能是基本視圖的一部分。

@Override 
protected View onCreateView(ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.base_preference, null); 

    if (mHint != null) { 
     TextView hintView = (TextView) layout.findViewById(R.id.PreferenceHintTextView); 
     hintView.setText(mHint); 
    } 

    TextView titleView = (TextView) layout.findViewById(R.id.PreferenceTitleTextView); 
    titleView.setText(getTitle()); 

    AutoCompleteTextView inputView = new AutoCompleteTextView(getContext()); 
    inputView.setGravity(Gravity.FILL_HORIZONTAL); 

    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), 
      R.layout.auto_complete_text_list_item, 
      getEntries()); 

    inputView.setAdapter(adapter); 
    inputView.setThreshold(1); 
    inputView.setOnItemSelectedListener(this); 
    layout.addView(inputView); 

    return layout; 
} 

列表項是:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:textColor="#000"> 
</TextView> 

可能已經可能已經標準的列表項,出來一樣。

+0

我有類似的問題,但在我的情況下,我使用簡單的EditView。你找到解決方案嗎? – noisy 2011-02-16 17:10:22

+0

一個演示項目將極大地幫助... – 2011-02-21 10:39:38

+0

對不起,我沒有找到解決方案。我只是將它改成了一個對話偏好,它可以像預期的那樣工作,並且因爲它耗費大量時間而移動。如果我回到它並整理出來,我會發布解決方案。 – Brian 2011-02-22 05:58:02

回答

0

我花了一點時間,試圖爲您重新創建此問題:

幾個問題:

  1. 什麼是的getContext() /獲取從上下文?
  2. 你能告訴你的R.layout.auto_complete_text_list_item?標準的列表項工作對我來說
  3. 您可以顯示getEntries()方法?經過一個小的String []數組測試之後,它對我有用。
  4. setOnItemSelectedListener(本) - 你能證明你使用這個,因爲它實現了OnItemSelectedListener方法 - 你可能需要requestFocus()方法在這裏或在做一些奇怪的拉焦點從控制而去...雖然這是爲選擇而不是點擊。我不確定這是否是由列表本身自動選擇觸發(即,默認爲第一項

最後,嘗試將組件設置爲xml,而不是動態地執行此操作以進行測試。這是我試圖控制的第一個因素,我知道你會希望這是動態的,但創建一個使用自己的佈局的自定義AutoCompleteTextView控件並不一定是壞事。我審查和協助。

+0

getContext()從類繼承的Preference繼承。 R.layout.auto_complete_text_list_item在上面,幾乎與標準列表項目相同。 getEntries直接從一個私有變量中返回一個CharSequence []。將OnItemSelectedListener的處理程序添加到主帖子中。 – Brian 2011-02-27 07:16:06

+0

謝謝。我正在嘗試重新創建這個設置。這有點奇怪,因爲您創建自定義首選項以及操縱主視圖...因此,您要麼擴展Preference類以生成此自定義AutoCompleteTextView(可能會添加到preferences.xml中),要麼此代碼位於PreferenceActivity包含所有的首選項?如果是後者,那麼你是不是想創建一個新的類作爲「AutoCompletePreference」來打開自己並處理用戶輸入,或者我是否在錯誤的切線?此外,我沒有看到您的帖子中的OnItemSelected處理程序... – 2011-02-28 02:11:01

0

我有同樣的問題,並假設首選項在一個ListView內處理。

因此,在獲得焦點時的OnFocusChangeListener()調用中,我調用此方法來解決問題。請注意,問題不會發生在每個設備或Android版本上,這可能會使其很難首先檢測到。

問題在運行Android 4.1.1的華碩設備上重現。

private boolean fixedAlready = false; 
private void fixListViewFocusability(View v) 
{ 
    if (fixedAlready) 
     return; 

    Log.d(at_data.TAG, "Trying to fix focus issues from " + v); 

    ViewGroup vg = (ViewGroup) v.getParent(); 
    while (vg != null) 
    { 
     if (vg instanceof ListView) 
     { 
      Log.d(at_data.TAG, "Found list view " + vg); 
      ListView lv = (ListView) vg; 

      fixedAlready = true; 
      lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS); 

      break; 
     } 

     vg = (ViewGroup) vg.getParent(); 
    } 
} 

上述工作適合我在該特定設備上。不知道其他設備如何反應。