android - getDialog 不是 return AlertDialog 的实例

android - getDialog does not return an instance of AlertDialog

我一直在开发 google 的课程阳光应用程序,并希望在其中加入我的个人风格,因此我通过使用此处显示的 EditTextPreference 和 AutoCompleteTextView 的混合来让用户指定他的城市:

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

所以一切都很顺利,直到我想禁用确定按钮的最后一部分

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

它 returns 一个不为空且不是 AlertDialog

实例的对话框

任何有关正确获取对话框或以编程方式禁用“确定”按钮的方法的任何帮助

找到问题就好了; 是我用的

import android.support.v7.app.AlertDialog;

而不是

import android.app.AlertDialog;

感谢所有试图提供帮助的人