我有一个开关按钮,设置如下:
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的新文本。如果ToggleButton已关闭(未选中),则文本将恢复为静态TextOff。
我怎样才能强制重画?我试过了
filterButton.postInvalidate();
但那没用。