Android - изменение текста положительной кнопки в диалоге EditTextPreference - PullRequest
4 голосов
/ 19 марта 2012

Я пытаюсь изменить positiveButtonText из Dialog в `EditTextPreference.Я пробовал следующее безуспешно:

    // The reference to the preference in the view heirarchy
    mUrlPreference = (EditTextPreference) getPreferenceScreen().findPreference(pref);       
    // Add a textwatcher
    mUrlPreference.getEditText().addTextChangedListener(prefWatcher);

prefWatcher выглядит следующим образом:

private TextWatcher prefWatcher = new TextWatcher() {

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

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.i(TAG_LOG, "Updating the positive button text");
        mUrlPreference.setPositiveButtonText("Download");
    }
};

Когда я изменяю текст в EditText, журнал печатается, ноПоложительный текст кнопки не меняется.Есть ли что-то, что я здесь скучаю?Сначала я думал, что мне придется обновить иерархию представления диалога, но я не вижу никаких методов в API для этого.Есть идеи, что мне здесь не хватает?

1 Ответ

1 голос
/ 19 марта 2012

Согласно документации setPositiveButtonText () , обновленный текст будет отображаться в последующих диалоговых окнах. Таким образом, чтобы действительно повлиять на кнопку, сделайте это вместо:

@Override
public void afterTextChanged(Editable s) {
    mUrlPreference.setPositiveButtonText("Download");

    Button button = (Button) mUrlPreference.getDialog().findViewById(android.R.id.button1);
    button.setText("Download");
    button.invalidate(); // if necessary
}
...