次のように設定された 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();
ましたが、役に立ちませんでした。