我有一个设置如下的 ToggleButton:
final ToggleButton filterButton = (ToggleButton) findViewById(R.id.filterTags);
filterButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (filterButton.isChecked()) {
// pop up the list of tags so the user can choose which to filter by
// once one is chosen, the spinner will be updated appropriately
showDialog(DIALOG_TAGS);
} else {
// going unpressed, set the the spinner list to everything
updateSpinner(db.itemNames());
}
}
});
对话框如下所示:
case DIALOG_TAGS:
final String[] tagNames = db.tagNamesInUse();
dialog = new AlertDialog.Builder(this)
.setItems(tagNames, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
updateSpinner(db.getItemNamesForTag(tagNames[which]));
final ToggleButton filterButton = (ToggleButton) findViewById(R.id.filterTags);
filterButton.setTextOn(tagNames[which]);
dialog.dismiss();
}
})
.setNegativeButton("Cancel", UITools.getDialogCancellingListener())
.create();
这个想法是:如果打开了 ToggleButton,它会弹出一个单选列表视图对话框,即标签列表。一旦选择了一个标签,它就会成为 ToggleButton 的新 textOn。如果 ToggleButton 已关闭(未选中),则文本将恢复为静态 TextOff。
问题是:一旦对话框消失,按钮就不会被重绘。显示的文本仍然是 textOn 的先前值。
如何强制重绘?我试过filterButton.postInvalidate();
了,但这没有帮助。