改变对话框后的活动

时间:2013-06-04 07:14:25

标签: android android-activity dialog callback fragment

我的应用中有一个对话框,用于更改字体大小和样式。我希望在此对话框中进行的更改应立即反映在活动下方,以便为用户提供设置预览。

为此,我已经从对话框中实现了回调。但它不起作用。任何帮助将不胜感激。

public class SettingsDialog extends DialogFragment implements OnSeekBarChangeListener {

SettingsDialogListener listener;
private int fontSize;
private String fontStyle;

SeekBar seekBar;

public void setListener(SettingsDialogListener listener) {
    this.listener = listener;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    loadDefaultValues();
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.settings_dialog, null);
    seekBar = (SeekBar) v.findViewById(R.id.font_size_seek_bar);
    seekBar.setOnSeekBarChangeListener(this);
    initializeSeekBar();

    builder.setView(v);
    builder.setTitle("Font");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            listener.onFontSettingsChanged(fontSize, fontStyle);
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            SettingsDialog.this.getDialog().cancel();
        }
    });

    return builder.create();
}

@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
    int minFontSize = FontSettings.getMinimumFontSize();
    int maxFontSize = FontSettings.getMaximumFontSize();
    double p = (progress * (maxFontSize - minFontSize)) / 100;
    fontSize = (int) (minFontSize + p);
    listener.onFontSettingsChanged(fontSize, fontStyle);

}

@Override
public void onStartTrackingTouch(SeekBar arg0) {

}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

}

public interface SettingsDialogListener {
    public void onFontSettingsChanged(int fontSize, String fontStyle);
}

private void loadDefaultValues() {
    fontSize = FontSettings.getFontSize(getActivity().getApplicationContext());
    fontStyle = FontSettings.getFontStyle(getActivity().getApplicationContext());
}

private void initializeSeekBar() {
    double percent = fontSize/FontSettings.getMaximumFontSize() * 100;
    seekBar.setProgress((int)percent);
}

在基础活动中:

@Override
public void onFontSettingsChanged(int fontSize, String fontStyle) {
    if (fragment != null)
        fragment.dosomething(fontSize,fontStyle);
}

0 个答案:

没有答案