1.解耦
2.代碼清晰,易維護
### 如何調用 dialogFragment
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
CustomDialogFragment customDialogFragment = new CustomDialogFragment();
customDialogFragment.show(fragmentManager, DIALOG_DATE);
---------------
### DIALOGFRAGMENT
package com.lepu.stethoscopic.fun.functiion.profile;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import com.lepu.stethoscopic.R;
import com.lepu.stethoscopic.config.UserConfig;
/**
* Created by Administrator on 2015/10/8.
* 公式 (40 - progress) * 0.5
*/
public class CustomDialogFragment extends DialogFragment implements SeekBar.OnSeekBarChangeListener {
private TextView textView;
private int lastValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lastValue = UserConfig.getConfigInt(getActivity(), "gainvalue", 40);//40 代表中間位置
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View viewDialog = getActivity().getLayoutInflater().inflate(R.layout.view_progress_bar, null);
final SeekBar seekBar = (SeekBar) viewDialog.findViewById(R.id.seekBar);
textView = (TextView) viewDialog.findViewById(R.id.gain_value);
seekBar.setMax(80);
//讀取出來上次儲存,進行初始化
seekBar.setProgress(lastValue);
textView.setText(lastValue > 40 ? "+" + (lastValue - 40) * 0.5 + " dB" : "-" + (40 - lastValue) * 0.5 + " dB");
seekBar.setOnSeekBarChangeListener(this);
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.settingzy)
.setView(viewDialog)
.setNegativeButton(R.string.cancel, null).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String value = textView.getText().toString();
String result = value.split("dB")[0];
//進行存儲
float floatValue = (Float.valueOf(result) + 20) * 2;
UserConfig.setConfig(getActivity(), "gainvalue", (int) floatValue);
//進行資料的處理
//UIHelper.showToast(getActivity(), "" + floatValue);
}
}).create();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress);
if (progress < 40) {
textView.setText("-" + (40 - progress) * 0.5 + " dB");
} else if (progress > 40) {
textView.setText("+" + (progress - 40) * 0.5 + " dB");
} else {
textView.setText("0.0 dB");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
布局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="left|center"
android:letterSpacing="10"
android:text="@string/gaintips" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1" />
<TextView
android:id="@+id/gain_value"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:gravity="center"
android:text="0.0 dB"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>