프로그래밍/Android

[Android]APIDemo ProgressBar 제작(UI 저장)

프리월드 2010. 10. 2. 23:54

커니님이 쓰신 Preferences를 이용한 UI저장을 이용하여 제작 하겠습니다.


public class ProgressBar1 extends Activity {
    // "pref"라는 가진 Shared Preferences 객체를 생성합니다.
   private SharedPreferences pref;
   private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Request the progress bar to be shown in the title
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.progressbar_1);
        setProgressBarVisibility(true);
        
        final ProgressBar progressHorizontal = 
(ProgressBar) findViewById(R.id.progress_horizontal);
        setProgress(progressHorizontal.getProgress() * 100);
       setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
        //저장
pref = getSharedPreferences("Pref", Activity.MODE_PRIVATE);
editor = pref.edit(); // 에디터를 받아옵니다.
//불러온당
ProgressBar.setProgress(pref.getInt("progressbar", 0));
        Button button = (Button) findViewById(R.id.increase);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());
//저장 관련
                editor.putInt("progressbar", ProgressBar.getProgress());
                editor.commit();
            }
        });

        button = (Button) findViewById(R.id.decrease);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());
//저장 관련
                editor.putInt("progressbar", ProgressBar.getProgress());
                editor.commit();
            }
        });

        button = (Button) findViewById(R.id.increase_secondary);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });

        button = (Button) findViewById(R.id.decrease_secondary);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(-1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });
        
    }
}


1. Shared Preferences 객체 생성하기
Intent 객체를 만들고, 그 안에 필요한 데이터를 넣어 보냈던 것 처럼 Shared Preferences 또한 객체를 생성해 주어야 합니다.
private SharedPreferences pref;
pref = getSharedPreferences("Pref", Activity.MODE_PRIVATE);

2. Shared Preferences를 수정할 수 있는 에디터 생성 및 편집
SharedPreferences 객체를 수정하려면, Editor 객체를 받아와야 합니다.
private SharedPreferences.Editor editor;
editor = pref.edit(); // 에디터를 받아옵니다.

ProgressBar progressHorizontal = 
(ProgressBar) findViewById(R.id.progress_horizontal);

editor.putInt("progressbar", ProgressBar.getProgress());
editor.commit();

3. 불러오기
불러오기를 해 줍시다
ProgressBar.setProgress(pref.getInt("progressbar", 0)); <- 뒤에 0은 default 값입니다.]

SecondaryProgressbar는 하지 않았습니다.
한번 해보시면서 복습 하시면 좋을 것 같네요.
부족하지만 지적해 주시면 수정하겠습니다.
감사합니다