|
안드로이드(Android) EditTextPreference 이용해서 옵션값 셋팅하는 방법 |
|
환경 : Eclipse Mars, Android 4.2.2 |
안드로이드에서 제공하는 EditTextPreference 위젯을 사용해서 옵션값을 셋팅하고 그 값을 읽어들여서 메인페이지에 셋팅하는 샘플입니다. 옵션페이지는 PreferenceActivity 를 상속받아서 구현하며 CheckBoxPreference, EditTextPreference 두 가지를 이용하였습니다.
▼ 아래는 메인페이지입니다. 옵션페이지에서 셋팅한 값을 가져와서 어떻게 셋팅했는지 결과값을 읽어들여 표시하는 기능이 있습니다. 옵션페이지에서는 체크박스와 에디터박스 두개가 있습니다.
▼ 메인페이지 소스입니다. 옵션창에서 셋팅한후 메인 페이지로 돌아왔을 때 바로 반영하기 위해 onResume() 함수를 오버라이드해서 사용했습니다. PreferenceManager.getDefaultSharedPreferences 함수를 이용해서 SharedPreferences 객체를 리턴받아 key/value 로 저장된 옵션값을 꺼내와서 화면에 표시해 줍니다.
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class EditTextActivity extends Activity {
TextView checkBoxStatus, editTextStatus;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
Button buttonSetPreference = (Button) findViewById(R.id.setpreference);
checkBoxStatus = (TextView) findViewById(R.id.checkboxstatus);
editTextStatus = (TextView) findViewById(R.id.edittextstatus);
buttonSetPreference
.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(
EditTextActivity.this,
PreferenceSetting.class));
}
});
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences prefer = PreferenceManager
.getDefaultSharedPreferences(this);
checkBoxStatus.setText("CheckBox 상태값 : "
+ prefer.getBoolean("checkboxvalue", true));
editTextStatus.setText("EditText 상태값 : "
+ prefer.getString("edittexvalue", ""));
}
}
▼ 아래 xml 소스는 메인페이지의 레이아웃입니다. 옵션창에서 읽어들인 값을 셋팅할 TextView 위젯이 추가 되어 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#3F0099"
android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="@string/hello"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/setpreference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_setprefer" />
<TextView
android:id="@+id/checkboxstatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/edittextstatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
▼ 두번째 페이지는 PreferenceActivity 클래스를 상속받아 구현한 페이지 입니다. Xml 에 CheckBoxPreference, EditTextPreference 위젯만 추가하면 별다른 구현없이 아래 그림과 같이 셋팅이 됩니다.
▼ EditTextPreference 위젯은 클릭하게 되면 값을 입력할수 있는 에디터박스가 뜨게 됩니다. 어렵게 다이얼로그 박스를 구현하지 않아도 됩니다. 값을 입력하고 OK 를 누르게 되면 xml 에 값이 저장됩니다.
▼ 옵션 페이지를 구현한 activity 소스입니다. Xml 파일을 읽어들일 때 트랜젝션 문제가 있을수 있으므로 onCreate 에 addPreferencesFromResource() 함수를 사용할수 없습니다. 별도로 PreferenceFragment 를 상속받아서 MyPreferenceFragment 클래스를 만들었습니다.
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class PreferenceSetting extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content,
new MyPreferenceFragment()).commit();
}
// PreferenceFragment 클래스 사용
public static class MyPreferenceFragment extends
PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting_edittext);
}
}
}
▼ 아래는 옵션화면에서 사용한 xml 소스 입니다. 이번예제에서는 두 개의 위젯이 들어가 있습니다. EditTextPreference 같은 경우 추가만 해두면 별도의 구현없이 EditText 다이얼로그를 사용해서 데이터를 저장할 수 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="checkboxvalue"
android:summary="CheckBoxPreference"
android:title="Checkbox" />
<EditTextPreference
android:key="edittexvalue"
android:summary="EditTextPreference"
android:title="EditText" />
</PreferenceScreen>
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) Genymotion 지니모션 이클립스에서 프로젝트 연결하는 방법 (1) | 2015.05.27 |
|---|---|
| 안드로이드(Android) Genymotion 지니모션 설치와 이클립스 플러그인 설치해서 연결 (1) | 2015.05.19 |
| [Android] 안드로이드 레이아웃 gravity, layout_gravity 가 적용이 안되는 경우 해결 (0) | 2015.05.15 |
| 안드로이드(Android) PreferenceActivity 에서 addPreferenceFromResource (0) | 2015.04.29 |
| 안드로이드(Android) PreferenceActivity 이용해서 사용자 정보 셋팅과 정보가져오기 (0) | 2015.03.26 |
| 안드로이드(Android) 스마트폰 맥주소(Mac Address) 확인하는 방법 (1) | 2015.03.09 |
| 안드로이드(Android) GPS 로 현재 이동 속도 알아오기 (5) | 2015.03.05 |
| 안드로이드(Android) Thread 를 이용해 로딩페이지 만들기 (2) | 2015.02.23 |
