안드로이드 스튜디오
안드로이드 스튜디오 데이터 저장 2 (SharedPreferences)
;세미콜론;
2020. 5. 3. 12:25
반응형
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="delete" />
</LinearLayout>
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text"
android:textSize="20dp"
android:inputType="textPersonName"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="red" />
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="green" />
<Button
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="blue" />
<Button
android:id="@+id/random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="random" />
</LinearLayout>
</LinearLayout>
package com.example.save;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.w3c.dom.Text;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private SharedPreferences Pre;
private SharedPreferences.Editor editor;
private int select;
Button save,delete,red,green,blue,radom;
EditText text;
Random r = new Random();
int red1 = r.nextInt(256),green1 = r.nextInt(256), blue1 = r.nextInt(256);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
save = findViewById(R.id.save);
delete = findViewById(R.id.delete);
red = findViewById(R.id.red);
green = findViewById(R.id.green);
blue = findViewById(R.id.blue);
radom = findViewById(R.id.random);
setListenerColorbtn();
setListenerPreferencebtn();
Pre = PreferenceManager.getDefaultSharedPreferences(this);
editor = Pre.edit();
initializeValue();
}
public void setListenerColorbtn(){
View.OnClickListener Listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.red:
select = Color.RED;
text.setTextColor(Color.RED);
break;
case R.id.blue:
select = Color.BLUE;
text.setTextColor(Color.BLUE);
break;
case R.id.green:
select = Color.GREEN;
text.setTextColor(Color.GREEN);
break;
case R.id.random:
select = Color.rgb(red1,green1,blue1);
text.setTextColor(Color.rgb(red1,green1,blue1));
break;
}
}
};
red.setOnClickListener(Listener);
blue.setOnClickListener(Listener);
green.setOnClickListener(Listener);
radom.setOnClickListener(Listener);
}
public void setListenerPreferencebtn() {
View.OnClickListener Listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
editor.putInt("color", select);
editor.putString("text", text.getText().toString());
editor.apply();
break; //데이터 저장
case R.id.delete:
editor.remove("color");
editor.remove("text");
editor.apply();
break; //데이터 삭제
}
}
};
save.setOnClickListener(Listener);
delete.setOnClickListener(Listener);
}
private void initializeValue() {
text.setText(Pre.getString("text","저장된 데이터가 없어요"));
text.setTextColor(Pre.getInt("color", Color.BLACK));
}
}
반응형