4

我試圖在EditTextPreference中更改DialogpositiveButtonText。我曾嘗試沒有成功如下:Android - 更改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

根據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 
} 
+0

謝謝!但你是如何得到正面按鈕的ID? – 500865 2012-03-19 17:57:56

+0

@ 500865 BUTTON_POSITIVE最初被稱爲[BUTTON1](http://developer.android.com/reference/android/content/DialogInterface.html#BUTTON1)。該id在[這裏]提到(http://developer.android.com/reference/android/R.id.html#button1)。 – 2012-03-19 18:01:47

相關問題