我试图在`EditTextPreference中更改Dialog的positiveButtonText.我试过以下但没有成功:
// 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 count,int after) { } @Override public void afterTextChanged(Editable s) { Log.i(TAG_LOG,"Updating the positive button text"); mUrlPreference.setPositiveButtonText("Download"); } };
当我更改EditText中的文本时,将打印日志,但正面按钮文本不会更改.这里有什么我想念的吗?我最初的想法是,我必须刷新对话框的视图层次结构,但我没有在API中看到任何方法来执行此操作.关于我在这里缺少什么的想法?
解决方法
根据
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 }