EditText에 값을 입력 받은 후 Gson을 사용하여 원하는 형식으로 출력하기
1. build.gradle에 코드 추가
implementation 'com.google.code.gson:gson:2.9.0'
2.activity_main.xml
코드
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이름을 입력하십시오"
android:id="@+id/edit_name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="번호를 입력하십시오"
android:id="@+id/edit_num"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="나이를 입력하십시오"
android:id="@+id/edit_age"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save_btn"
android:text="값 저장하기"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rtn_btn"
android:text="값 불러오기"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_info"
android:text="값"
android:textSize="20sp"
/>
</LinearLayout>
3.Info.java
java 파일 생성
package com.example.gson_ex;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Info {
private String name;
private int num;
private int age;
public Info(String name, int num, int age){
this.name = name;
this.age = age;
this.num = num;
}
public String toString(){
return "정보[이름 : "+ name+", 나이 : "+age+",번호 : "+num+"]";
}
}
4.MainActivity.java
package com.example.gson_ex;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
Gson gson;
String name;
int num;
int age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editName = (EditText) findViewById(R.id.edit_name);
EditText editAge = (EditText) findViewById(R.id.edit_age);
EditText editNum = (EditText) findViewById(R.id.edit_num);
Button saveBtn = (Button) findViewById(R.id.save_btn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = editName.getText().toString();
num = Integer.parseInt(editNum.getText().toString());
age = Integer.parseInt(editAge.getText().toString());
}
});
Button rtnBtn = (Button) findViewById(R.id.rtn_btn);
rtnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Info info = new Info(name,num,age);
gson = new Gson();
String infoJson = gson.toJson(info.toString());
TextView textInfo = (TextView) findViewById(R.id.text_info);
textInfo.setText(infoJson);
System.out.println(infoJson);
}
});
}
}
5, 동작
1) 값 저장하기 클릭
2) 값 불러오기 클릭
'Android' 카테고리의 다른 글
Android] Scroll View 사용 (0) | 2022.05.02 |
---|---|
Android] Shared Preferences를 이용한 간단한 회원 가입 (0) | 2022.04.05 |
Android] Gson의 정의 및 사용 방법 (0) | 2022.03.30 |
Android] SharedPreferences 저장 위치 접근 (0) | 2022.03.24 |
Android] JSON 생성 및 파싱 (2) | 2022.03.20 |