728x90
반응형
프레퍼런스 읽기
SharedPreferences pref = getSharedPreferences("sharedpreferences", MODE_PRIVATE);
boolean bSound = pref.getBoolean("sound", false);
프레퍼런스 쓰기
SharedPreferences pref = getSharedPreferences("sharedpreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("sound", bChecked);
editor.commit();
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="읽기"
android:textSize="30dp"
android:id="@+id/btn_read"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="쓰기"
android:layout_marginLeft="10dp"
android:textSize="30dp"
android:id="@+id/btn_write"/>
</LinearLayout>
package com.example.test01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_read).setOnClickListener(lis_read);
findViewById(R.id.btn_write).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences("sharedpreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("sound", true);
editor.putString("name", "hello");
editor.commit();
}
});
}
View.OnClickListener lis_read = new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = 0;
FileInputStream fis = null;
String FILENAME = "myFile.txt";
try {
fis = openFileInput(FILENAME);
int len = fis.available();
if(len > 0) {
byte buff[] = new byte[len];
while (result > -1) {
result = fis.read(buff);
}
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/ll_menu"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="저장하기"
android:id="@+id/btn_save"
android:onClick="onClickSave"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="읽어오기"
android:id="@+id/btn_read"
android:onClick="onClickRead"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력모드"
android:id="@+id/btn_input"
android:onClick="onClickInput"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll_menu"
android:visibility="invisible"
android:id="@+id/et_input"
/>
<TextView
android:id="@+id/tv_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll_menu"
android:inputType="none"
android:visibility="invisible" />
</RelativeLayout>
package com.example.example01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
// 읽고 저장할 파일의 파일명을 상수 FILENAME에 저장
static final String FILENAME = "myFile.txt";
EditText et_input;
TextView tv_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_input = (EditText) findViewById(R.id.et_input);
tv_data = (TextView) findViewById(R.id.tv_data);
}
public void onClickSave(View v) {
// 에디트 텍스트의 입력된 내용을 str_input에 저장
String str_input = et_input.getText().toString();
// 내부저장소의 myFile.txt 파일에 str_input을 저장
FileOutputStream fos = null;
}
}
키-값 저장, 공유 프레퍼런스
공유 프레퍼런스, dialog
20191281.notion.site
▪️ 만두은진 참고❤️
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - ListView와 SQLite (0) | 2022.08.24 |
---|---|
Android Studio - SQLite 데이터베이스 (0) | 2022.08.24 |
Android Studio - 공유 프레퍼런스, dialog (0) | 2022.08.11 |
Android Studio - 어싱크 태스크와 타이머 (0) | 2022.08.11 |
Android Studio - 스레드와 핸들러 (0) | 2022.08.11 |
댓글