2010-07-24 27 views
7

Có thể có EditTextPreference với AutoComplete được đính kèm không?Có thể tự động hoàn thành EditTextPreference không?

Tôi biết ho để đính kèm một thành phần tử với id, nhưng tôi gặp khó khăn trong việc tìm hiểu cách đính kèm ArrayAdapter vào trường tùy chọn.

Điều này là sai, nhưng nó gần như tôi có thể nhận được.

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); 

Trả lời

0

Có lẽ nếu bạn phân lớp nó và làm cho cái nhìn của riêng bạn cho điều đó và sử dụng đối tượng AutoCompleteTextView như yếu tố đó sẽ làm việc, như hiện nay tôi không thấy làm thế nào một EditText đơn giản có thể được thay đổi để autocomplete.

8

Đây là giải pháp thay thế mà tôi đã triển khai bằng cách nghiên cứu mã nguồn EditTextPreference.java.

Về cơ bản bạn cần phải phân lớp EditTextPreference và ghi đè khi nó liên kết với hộp thoại. Tại thời điểm này, bạn có thể truy xuất EditText, sao chép giá trị của nó và xóa nó khỏi nhóm xem cha mẹ của nó. Sau đó, bạn tiêm Autocompletetextview của bạn và treo lên nó 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

Dường như với tôi phải có cách "dễ dàng hơn" để thực hiện việc này hơn là xâm nhập vào lớp EditTextPreference và làm rối tung lượt xem. Đây là giải pháp của tôi, vì AutoCompleteTextView mở rộng EditText, tôi chỉ phải ghi đè lên các phương thức EditTextPreference gọi trực tiếp đối tượng EditText liên tục của chúng.

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); 
     } 
    } 
} 
} 

Nhờ Brady để liên kết với nguồn.

+0

Hầu như! Tôi nhận được hộp hoàn thành tự động của tôi xuất hiện, nhưng hộp thả xuống hoàn toàn tự động bị cắt và hiển thị phía trên trường nhập với nửa dưới của danh sách thả xuống không hiển thị. –

+0

Trên thực tế, tôi đã có thể sửa lỗi của mình từ nhận xét trước bằng cách mã hóa cứng một giá trị cho chiều cao hộp thả xuống. –

Các vấn đề liên quan