0

Google의 코스 선샤인 앱에서 작업 중이며 개인 터치를 사용하여 사용자가 자신의 도시를 EditTextPreference와 AutoCompleteTextView의 하이브리드를 사용하여 지정하도록했습니다. 여기 : 내가)android - getDialog가 AlertDialog의 인스턴스를 반환하지 않습니다.

private void validate() { 
    dialog = getDialog(); 
    Toast.makeText(getContext(), Boolean.toString(dialog instanceof AlertDialog), Toast.LENGTH_SHORT).show(); 

    if (dialog instanceof AlertDialog) { 
     Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE); 
     btn.setEnabled(isValid); 
    } 
} 

그래서 내가 (방법 getDialog를 시도 확인 버튼을 비활성화 싶었던

public class AutoCompleteEditTextPreference extends EditTextPreference { 

private static String[] list; 
private boolean isValid = true; 
private Dialog dialog; 

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


    Arrays.sort(list); 

    isValid = isValid(mACTV.getText().toString()); 


    mACTV.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      isValid = isValid(s.toString()); 
      validate(); 
     } 
    }); 

    mACTV.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      isValid = isValid(mACTV.getText().toString()); 
      validate(); 
     } 
    }); 

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), 
      android.R.layout.simple_dropdown_item_1line, list); 
    mACTV.setAdapter(adapter); 

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

private boolean isValid(CharSequence text) { 
    return !text.equals("") && Arrays.binarySearch(list, text.toString()) > 0; 
} 

@Override 
protected void showDialog(Bundle state) { 
    super.showDialog(state); 
    validate(); 
} 

private void validate() { 
    dialog = getDialog(); 
    Toast.makeText(getContext(), Boolean.toString(dialog instanceof AlertDialog), Toast.LENGTH_SHORT).show(); 

    if (dialog instanceof AlertDialog) { 
     Button btn = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE); 
     btn.setEnabled(isValid); 
    } 
} 

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


static void prepareCountriesList(Context context) { 

    List<String> lines = new ArrayList<>(); 

    try { 

     InputStream inputStream = context.getAssets().open("cities.txt"); 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 

     String line; 

     while ((line = bufferedReader.readLine()) != null) { 
      lines.add(line); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    list = lines.toArray(new String[lines.size()]); 
} 

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

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

그래서 모든 마지막 부분까지 잘 가고 있었다;

과 제대로 대화 상자를 얻거나 확인 버튼을 비활성화하는 또 다른 방법에 null이 아닌 대화와에 AlertDialog

아닌 인스턴스

anyhelp하십시오를 반환 프로그램

답변

0

그것은 문제가 발견 괜찮습니다; 내가

을 돕기 위해 노력하는 사람들 누구나

import android.app.AlertDialog; 

감사의

import android.support.v7.app.AlertDialog; 

대신 사용하는 것이 었습니다

관련 문제