[kt] //데이터 저장 val pref = PreferenceManager.getDefaultSharedPreferenced(this) val editor = pref.edit() editor.putInt("KEY1", value) .putInt("KEY2", value2) .apply() //프리퍼런스 매니저를 사용해 프리퍼런스 객체를 얻은 후 에디터 객체를 얻음 //데이터 객체에 put[데이터 타입] 형식의 메서드를 사용하여 키와 값 형태의 쌍으로 저장을 한다. //put 메서드는 기본 타입은 모두 지원함. //apply()로 설정한 내용을 반영 //데이터 가져오기 val pref = PreferenceManager.getDefaultSharedPreferenced(this) …
[카테고리:] Android
Intent로 새 액티비티 띄우는 코드 with Data → Anko
[kt] //기존의 코드 val intent = Intent(this, ResultActivity::class.java) startActivity(intent) //Anko 라이브러리 코드 startActivity<ResultActivity>() //와우!! ———————————————— //데이터 전달 시 Java 코드 val intent = Intent(this, ResultActivity::class.java) intent.putExtra("weight", weightEditText.text.toString()) intent.putExtra("height", heightEditText.text.toString()) startActivity(intent) //Anko 적용 시 startActivity<ResultActivity>( "weight" to weightEditText.text.toString(), "height" to heightEditText.text.toString() ) ———————————————— //데이터 받는 방법 val height = intent.getStringExtra("height").toInt() val weight = intent.getStringExtra("weight").toInt() [/kt]
프로젝트가 안드로이드 5.0 미만일 때 Vector Drawable 표시되게 하는 방법
[kt] //모듈 수준의 gradle 파일에 defaultConfig { vectorDrawables.useSupportLibrary = true } [/kt]
Android 합성프로퍼티로 뷰 인스턴스 접근
//다음 코드와 같이 java의 findViewById()를 이용하지 않고 바로 접근 가능 [kt] import kotlinx.android.synthetic.main.activity_main.* tv_text.text = "Hello Kotlin!" [/kt]