2010-07-24 17 views
7

क्या इसके साथ जुड़े ऑटोकंपलेट के साथ एडिटटेक्स्ट प्रेफर होना संभव है?एक EditTextPreference स्वत: पूर्ण करने के लिए संभव है?

मुझे पता है कि एक आईडी के साथ एक तत्व को संलग्न करने के लिए हो, लेकिन मुझे पता है कि ArrayAdapter को वरीयता फ़ील्ड में कैसे संलग्न किया जाए।

यह गलत है, लेकिन यह जितना करीब हो सकता है उतना करीब है।

final String[] TEAMS = getResources().getStringArray(R.array.teams); 
AutoCompleteTextView EditTextPreference = (AutoCompleteTextView) findViewById(R.id.editTextPrefTeam);  
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, TEAMS); 
EditTextPreference.setAdapter(adapter); 

उत्तर

0

शायद अगर आप इसे उपवर्ग और उस के लिए अपने स्वयं के दृश्य बनाने के लिए और तत्व के रूप में AutoCompleteTextView वस्तु यह काम करेंगे उपयोग करते हैं, वर्तमान में मैं नहीं दिख रहा है के रूप में कैसे एक सरल EditText स्वत: पूर्ण करने के लिए बदला जा सकता है।

8

यहां एक कामकाज है जिसे मैंने EditTextPreference.java स्रोत कोड का अध्ययन करके कार्यान्वित किया है।

अनिवार्य रूप से आपको EditTextPreference को उपclass करने की आवश्यकता है और जब यह संवाद से जुड़ा होता है तो ओवरराइड करने की आवश्यकता होती है। इस बिंदु पर आप EditText को पुनर्प्राप्त कर सकते हैं, इसके मानों की प्रतिलिपि बना सकते हैं, और इसे अपने मूल दृश्य समूह से हटा सकते हैं। फिर आप अपने Autocompletetextview इंजेक्ट करते हैं और इसके Arrayadapter को हुक करते हैं।

public class AutoCompleteEditTextPreference extends EditTextPreference 
{ 
    public AutoCompleteEditTextPreference(Context context) 
    { 
     super(context); 
    } 

    public AutoCompleteEditTextPreference(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public AutoCompleteEditTextPreference(Context context, AttributeSet attrs, 
     int defStyle) 
    { 
     super(context, attrs, defStyle); 
    }  

    /** 
    * the default EditTextPreference does not make it easy to 
    * use an AutoCompleteEditTextPreference field. By overriding this method 
    * we perform surgery on it to use the type of edit field that 
    * we want. 
    */ 
    protected void onBindDialogView(View view) 
    { 
     super.onBindDialogView(view); 

     // find the current EditText object 
     final EditText editText = (EditText)view.findViewById(android.R.id.edit); 
     // copy its layout params 
     LayoutParams params = editText.getLayoutParams(); 
     ViewGroup vg = (ViewGroup)editText.getParent(); 
     String curVal = editText.getText().toString(); 
     // remove it from the existing layout hierarchy 
     vg.removeView(editText);   
     // construct a new editable autocomplete object with the appropriate params 
     // and id that the TextEditPreference is expecting 
     mACTV = new AutoCompleteTextView(getContext()); 
     mACTV.setLayoutParams(params); 
     mACTV.setId(android.R.id.edit); 
     mACTV.setText(curVal); 


     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), 
      android.R.layout.simple_dropdown_item_1line, [LIST OF DATA HERE]); 
     mACTV.setAdapter(adapter); 

     // add the new view to the layout 
     vg.addView(mACTV); 
    } 

    /** 
    * Because the baseclass does not handle this correctly 
    * we need to query our injected AutoCompleteTextView for 
    * the value to save 
    */ 
    protected void onDialogClosed(boolean positiveResult) 
    { 
     super.onDialogClosed(positiveResult); 

     if (positiveResult && mACTV != null) 
     {   
      String value = mACTV.getText().toString(); 
      if (callChangeListener(value)) { 
       setText(value); 
      } 
     } 
    } 

    /** 
    * again we need to override methods from the base class 
    */ 
    public EditText getEditText() 
    { 
     return mACTV; 
    } 

    private AutoCompleteTextView mACTV = null; 
    private final String TAG = "AutoCompleteEditTextPreference"; 
} 
8

यह मुझे लग रहा था वहाँ एक "आसान" जिस तरह से EditTextPreference वर्ग में हैकिंग और दृश्य के साथ खिलवाड़ द्वारा की तुलना में यह पूरा करने के लिए किया जाना था। यहां मेरा समाधान है, क्योंकि AutoCompleteTextView EditText को बढ़ाता है, मुझे केवल EditTextPreference विधियों को ओवरराइड करना पड़ता है जो उनके निरंतर संपादन टेक्स्ट ऑब्जेक्ट को सीधे कॉल करते हैं।

public class AutoCompletePreference extends EditTextPreference { 

private static AutoCompleteTextView mEditText = null; 

public AutoCompletePreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mEditText = new AutoCompleteTextView(context, attrs); 
    mEditText.setThreshold(0); 
    //The adapter of your choice 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    mEditText.setAdapter(adapter); 
} 
private static final String[] COUNTRIES = new String[] { 
    "Belgium", "France", "Italy", "Germany", "Spain" 
}; 

@Override 
protected void onBindDialogView(View view) { 
    AutoCompleteTextView editText = mEditText; 
    editText.setText(getText()); 

    ViewParent oldParent = editText.getParent(); 
    if (oldParent != view) { 
     if (oldParent != null) { 
      ((ViewGroup) oldParent).removeView(editText); 
     } 
     onAddEditTextToDialogView(view, editText); 
    } 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 
    if (positiveResult) { 
     String value = mEditText.getText().toString(); 
     if (callChangeListener(value)) { 
      setText(value); 
     } 
    } 
} 
} 

स्रोत से जोड़ने के लिए ब्रैडी के लिए धन्यवाद।

+0

लगभग! मुझे अपना ऑटो पूर्ण बॉक्स दिखाई देने के लिए मिलता है, लेकिन ऑटो पूर्ण ड्रॉप डाउन बॉक्स काट दिया जाता है और ड्रॉप डाउन सूची के निचले हिस्से के साथ इनपुट फ़ील्ड के ऊपर दिखाया जाता है। –

+0

दरअसल, मैं ड्रॉपडाउन बॉक्स ऊंचाई के लिए मूल्य को हार्ड कोडिंग करके पिछली टिप्पणी से अपनी बग को ठीक करने में सक्षम था। –

संबंधित मुद्दे