How can I get working dynamic ToggleButton text under android?

泪湿孤枕 提交于 2019-12-08 14:46:21

问题


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());
                }
            }
        });

and the dialog looks like:

   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();

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.

The problem is: the button isn't being redrawn once the dialog goes away. The text showing is still the previous value of textOn.

How can I force a redraw? I tried filterButton.postInvalidate(); but that didn't help.


回答1:


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!



来源:https://stackoverflow.com/questions/3784292/how-can-i-get-working-dynamic-togglebutton-text-under-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!