Secret Diary

Secret Diary

사용 기술 : Handler, Theme, SharedPreference, AlertDialog

// MainActivity.kt package oh.hee.secretdiary import android.content.Context import android.content.DialogInterface import android.content.Intent import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.NumberPicker import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.AppCompatButton import androidx.core.content.edit class MainActivity : AppCompatActivity() { private val numberPicker1 by lazy { findViewById(R.id.numberPicker1) .apply { minValue = 0 maxValue = 9 } } private val numberPicker2 by lazy { findViewById(R.id.numberPicker2) .apply { minValue = 0 maxValue = 9 } } private val numberPicker3 by lazy { findViewById(R.id.numberPicker3) .apply { minValue = 0 maxValue = 9 } } private val openButton by lazy { findViewById(R.id.openButton) } private val changePasswordButton by lazy { findViewById(R.id.changePasswordButton) } private var changePasswordMode = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) numberPicker1 numberPicker2 numberPicker3 openButton.setOnClickListener { if (changePasswordMode){ Toast.makeText(this,"비밀번호 변경 중입니다.",Toast.LENGTH_SHORT).show() return@setOnClickListener } val passwordPreferences = getSharedPreferences("password", Context.MODE_PRIVATE) val passwordFromUser = "${numberPicker1.value}${numberPicker2.value}${numberPicker3.value}" if (passwordPreferences.getString("password","000").equals(passwordFromUser)){ startActivity(Intent(this,DiaryActivity::class.java)) }else{ // 실패 showErrorAlertDialog() } } changePasswordButton.setOnClickListener { val passwordPreferences = getSharedPreferences("password", Context.MODE_PRIVATE) val passwordFromUser = "${numberPicker1.value}${numberPicker2.value}${numberPicker3.value}" if (changePasswordMode){ passwordPreferences.edit(true){ putString("password",passwordFromUser) } changePasswordMode = false changePasswordButton.setBackgroundColor(Color.BLACK) }else { // changePasswordMode 가 활성화 :: 비밀번호가 맞는지를 체크 if (passwordPreferences.getString("password","000").equals(passwordFromUser)){ changePasswordMode = true Toast.makeText(this, "변경할 패스워드를 입력해주세요", Toast.LENGTH_SHORT).show() changePasswordButton.setBackgroundColor(Color.RED) }else{ showErrorAlertDialog() } } } } private fun showErrorAlertDialog(){ AlertDialog.Builder(this) .setTitle("실패!!") .setMessage("비밀번호가 잘못되었습니다.") .setPositiveButton("확인"){ _: DialogInterface, _: Int -> } .create() .show() } }

// DiaryActivity.kt package oh.hee.secretdiary import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log import android.widget.EditText import androidx.core.content.edit import androidx.core.widget.addTextChangedListener class DiaryActivity : AppCompatActivity() { private val diaryEditText by lazy { findViewById(R.id.diaryEditText) } private val handler = Handler(Looper.getMainLooper()) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_diary) val detailPreferences = getSharedPreferences("diary", Context.MODE_PRIVATE) diaryEditText.setText(detailPreferences.getString("detail","")) val runnable = Runnable { getSharedPreferences("diary", Context.MODE_PRIVATE).edit{ putString("detail",diaryEditText.text.toString()) } Log.d("DiaryActivity", "TextChanged :: ${diaryEditText.text.toString()}") } diaryEditText.addTextChangedListener { handler.removeCallbacks(runnable) handler.postDelayed(runnable, 500) } } }

from http://jhg3410.tistory.com/40 by ccl(A) rewrite - 2021-12-26 19:01:36