我怎样才能在Android平台的动态切换按钮的文字? [英] How can I get working dynamic ToggleButton text under android?
问题描述
我已经得到了的成立就像一个切换按钮:
I've got a ToggleButton that's set up like:
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();
的理念是:如果切换按钮被接通时,它会弹出单选择列表视图对话框这标记清单。一旦标签被选中,成为了切换按钮新的纹元。如果切换按钮被关闭(未选中),然后将文本恢复到静态TextOff。
The idea is: if the ToggleButton is turned on, it pops up a single-choice listview dialog that's the list of tags. Once a tag is chosen, it becomes the new textOn for the ToggleButton. If the ToggleButton is turned off (unChecked), then the text reverts to the static TextOff.
问题是:一旦该对话框消失的按钮是不是被重新绘制。文字显示还是纹元的previous值。
The problem is: the button isn't being redrawn once the dialog goes away. The text showing is still the previous value of textOn.
我怎么能强制重绘?我试图 filterButton.postInvalidate();
但这并没有帮助
How can I force a redraw? I tried filterButton.postInvalidate();
but that didn't help.
推荐答案
解决了!源头上切换按钮的明智读数显示,虽然setTextOn()和setTextOff()不会导致(私)syncTextState该更新的TextView位的呼叫,呼叫setChecked()的确实的。因此,关键是:
Solved! Judicious reading of the source to ToggleButton shows that while setTextOn() and setTextOff() don't cause a call to (private) syncTextState which updates the TextView bits, calling setChecked() does. So the trick is:
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]);
filterButton.setChecked(filterButton.isChecked());
dialog.dismiss();
}
})
这工作得相当好。耶开源!
Which worked quite nicely. Yay for open source!
这篇关于我怎样才能在Android平台的动态切换按钮的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!