Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- snowpack
- 버전일치
- degit
- parcel
- SQL
- 상속
- Spring
- kotlin
- sub query
- FIle
- 시큐어코딩
- 스프링
- 숫자
- 안드로이드
- git
- java
- 미니게임
- 오버라이드
- 스타일보험
- 답글
- 함수
- webpack
- java#왕초보
- React
- 쿠키
- Android
- Spinner
- 왕초보
- 게시판
- 코틀린
Archives
- Today
- Total
YSHUSH
화면 전환(Preference) 본문
텍스트를 입력하고
다른 화면으로 이동해서 값을 읽어보도록 하자
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="485dp"
android:layout_height="74dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.198"/>
<Button
android:text="프리퍼런스 쓰기"
android:layout_width="485dp"
android:layout_height="76dp"
android:id="@+id/write"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintVertical_bias="0.077"/>
<Button
android:text="다음 화면으로 이동"
android:layout_width="485dp"
android:layout_height="76dp"
android:id="@+id/move"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintVertical_bias="0.235"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. app - src - main - java내 메인패키지 우클릭 - 새로만들기 - Activity - Empty Activity - SecondActivity로 kt파일생성(activity_second 자동생성됨)
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:text="TextView"
android:textSize="30dp"
android:layout_width="310dp"
android:layout_height="85dp"
android:id="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.179"/>
<Button
android:text="프리퍼런스 읽기"
android:layout_width="310dp"
android:layout_height="81dp"
android:id="@+id/read"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="36dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.kt
package com.example.sample33
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val write = findViewById<Button>(R.id.write)
val move = findViewById<Button>(R.id.move)
val edit = findViewById<EditText>(R.id.editText)
write.setOnClickListener{
val pref = getSharedPreferences("pref", MODE_PRIVATE)
// pref에 대한 공간확보
val editor = pref.edit()
// 짐싸기
editor.putString("mydata", edit.text.toString())
// 저장
editor.commit()
// 초기화
edit.setText("")
}
move.setOnClickListener{
val i = Intent(this, SecondActivity::class.java)
startActivity(i)
}
}
}
4. SecondActivity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val read = findViewById<Button>(R.id.read)
val textView = findViewById<TextView>(R.id.textView)
read.setOnClickListener{
val pref = getSharedPreferences("pref", MODE_PRIVATE)
textView.text = pref.getString("mydata", "")
}
}
}
'Coding > Android(kotlin)' 카테고리의 다른 글
싱글턴 (Singleton) (0) | 2022.02.10 |
---|---|
화면 전환(Intent) (0) | 2022.02.10 |
JSON (0) | 2022.02.10 |
File (0) | 2022.02.10 |
Counter(숫자 세기 미니 프로그램) (0) | 2022.02.08 |