16

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

4

1 に答える 1

20

解決しました!ToggleButton のソースを慎重に読むと、setTextOn() と setTextOff() は TextView ビットを更新する (プライベート) syncTextState の呼び出しを引き起こしませんが、setChecked() を呼び出すと . したがって、トリックは次のとおりです。

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

これは非常にうまく機能しました。オープンソースをよろしく!

于 2010-09-25T04:58:29.647 に答える