Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 예외처리
- JSP
- Android
- 이클립스
- paint
- layout
- mybatis
- struts2
- 안드로이드
- 배열
- 기본
- 어노테이션
- JavaScript
- 메서드
- oracle
- OGNL
- 에러페이지
- AWT
- 클래스
- Menu
- Graphic
- 전화걸기
- 생성자
- 국제화
- Eclips
- Spring
- 메소드
- Java
- 오버로딩
- HTML
Archives
- Today
- Total
note
안드로이드 환경설정Preferences , 메뉴 본문
메인에는 환경설정값을 가져옵니다 (없을시에는 기본값 지정)
환경설정 진입화면
상세화면 진입화면
res > 에 xml 폴더 생성후 -> preferences.xml을 생성합니다
(마우스 몇번만 누르면 가능해서 캡쳐는 생략합니다)
main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:text="체크박스:" android:paddingRight="5px" /> <TextView android:id="@+id/checkbox" /> </TableRow> <TableRow> <TextView android:text="벨소리:" android:paddingRight="5px" /> <TextView android:id="@+id/ringtone" /> </TableRow> <TableRow> <TextView android:text="체크박스 #2:" android:paddingRight="5px" /> <TextView android:id="@+id/checkbox2" /> </TableRow> </TableLayout>
preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Simple Preferences"> <CheckBoxPreference android:key="@string/checkbox" android:title="체크박스 환경설정" android:summary="체크 상태를 변경합니다" /> <RingtonePreference android:key="@string/ringtone" android:title="벨소리 환경설정" android:showDefault="true" android:showSilent="true" android:summary="벨소리를 선택합니다" /> </PreferenceCategory> <PreferenceCategory android:title="상세 화면"> <PreferenceScreen android:key="detail" android:title="상세 화면" android:summary="별도 페이지에 상세 설정이 있습니다"> <CheckBoxPreference android:key="@string/checkbox2" android:title="상세 체크박스" android:summary="체크 상태를 변경합니다" /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
main javafile
package com.commonsware.android.prefs;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class StructuredPrefsDemo extends Activity {
private static final int EDIT_ID =1;
private static final int CLOSE_ID =2;
private TextView checkbox = null;
private TextView ringtone = null;
private TextView checkbox2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkbox = (TextView)findViewById(R.id.checkbox);
ringtone = (TextView)findViewById(R.id.ringtone);
checkbox2 = (TextView)findViewById(R.id.checkbox2);
}
@Override
public void onResume(){
super.onResume();
//환경설정시 생성된 default 환경설정 파일을 읽어들여 SharedPreference 객체 생성
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//환경설정 파일에서 저장된 정보의 타입별로 메소드를 호출해 정보를 추출함
//getBoolean(key,기본값)
// 기본값 : key를 통해서 value를 호출하게 되는데
// 설정된 값이 없을때 보여지는 값
checkbox.setText(new Boolean(prefs.getBoolean("checkbox",false)).toString());
ringtone.setText(prefs.getString("ringtone",""));
checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2",false)).toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "환경 설정")
.setIcon(R.drawable.misc);
menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "닫기")
.setIcon(R.drawable.eject);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case EDIT_ID:
startActivity(new Intent(this,EditPreferences.class));
return true;
case CLOSE_ID:
finish();
return true;
}
return(super.onOptionsItemSelected(item));
}
}
sub javafile
package com.commonsware.android.prefs;
import android.preference.PreferenceActivity;
import android.os.Bundle;
public class EditPreferences extends PreferenceActivity{
@Override
public void onCreate(Bundle savedIstanceState){
super.onCreate(savedIstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
'Android > 기본' 카테고리의 다른 글
| Android Paint로 그림그리기 (0) | 2012.02.01 |
|---|---|
| Android Preferences 읽고 쓰기(환경설정) (0) | 2012.02.01 |
| RSS 가져오기(뉴스) (1) | 2012.01.31 |
| Progressbar로 로딩표현 (인터넷로딩) (0) | 2012.01.31 |
| 안드로이드 서비스 동작중인 서비스 인지 체크 (0) | 2012.01.30 |