375
No
class MainActivity
package com.example.choi.mystudy25_3;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etCountry, etLanguage;
Button btnSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCountry=(EditText)findViewById(R.id.et_country);
etLanguage=(EditText)findViewById(R.id.et_language);
btnSet=(Button)findViewById(R.id.bt_set);
// 파일저장
btnSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//파일이 없으면 자동으로 만들어 준다.
//getSharedPreferences(String name, int mode)
SharedPreferences sp =getSharedPreferences("preference", 0);
SharedPreferences.Editor editor=sp.edit();
String country=etCountry.getText().toString();
String language=etLanguage.getText().toString();
editor.putString("country", country);
editor.putString("language", language);
editor.commit();
}
});
init();
}
//초기값 설정
private void init(){
//preference 의 임의 값을 가져온다. 없으면 생성
SharedPreferences sp =getSharedPreferences("preference", 0);
//preference 저장된 country 값을 가졍 온다. 없으면 country 에 korea 로 생성
// String getString(String key, @Nullable String defValue);
// 값이 없으면 디폴트 값이 korea 이다.
String country =sp.getString("country", "korea");
String language =sp.getString("language", "hangeul");
etCountry.setText(country);
etLanguage.setText(language);
}
// 종료시 자동 저장
@Override
protected void onPause() {
super.onPause();
SharedPreferences sp =getSharedPreferences("preference", 0);
SharedPreferences.Editor editor=sp.edit();
String country=etCountry.getText().toString();
String language=etLanguage.getText().toString();
editor.putString("country", country);
editor.putString("language", language);
editor.commit();
Toast.makeText(MainActivity.this, "종료시 자동 저장", Toast.LENGTH_SHORT).show();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.choi.mystudy25_3.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="국가"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_weight="1"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="@color/colorAccent" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/et_country"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="언어"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_weight="1"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textSize="24sp"
android:textColor="@color/colorAccent" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/et_language"
android:layout_weight="4" />
</LinearLayout>
<Button
android:text="setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_set"
android:background="@android:color/holo_green_dark"
android:textColor="@android:color/background_light" />
</LinearLayout>
댓글 ( 4)
댓글 남기기