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 |
Tags
- JS
- stylesheet
- SpringBoot
- TypeScript
- Kotlin
- RecyclerView
- scrollview
- Java
- button
- Android
- Javscript
- 구글맵스
- 자바스크립트
- React
- GoogleMaps
- Linux
- 코틀린
- nodejs
- array
- fragment
- 스프링부트
- Hook
- npm
- 리액트
- TextView
- 오버라이딩
- 안드로이드
- 랜덤넘버
- 랜덤번호
- JavaScript
Archives
- Today
- Total
타닥타닥 개발자의 일상
Kotlin 정렬된 grid 안드로이드 화면에 표시하기, grid에서 선택한 사항 textview랑 연결하기 본문
코딩 기록/Kotlin
Kotlin 정렬된 grid 안드로이드 화면에 표시하기, grid에서 선택한 사항 textview랑 연결하기
NomadHaven 2022. 2. 7. 17:29폴더 및 파일 구성
layout 폴더에 item_spinner.xml항목 추가
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingTop="10dp"
android:paddingStart="30dp"
android:textColor="@android:color/darker_gray"
android:textSize="15sp"
android:paddingLeft="30dp"/>
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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.031"
/>
<GridView
android:id="@+id/grid"
android:numColumns="auto_fit"
android:columnWidth="100dp"
android:layout_width="531dp"
android:layout_height="773dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintVertical_bias="0.839"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.sample20
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.GridView
import android.widget.TextView
class MainActivity : AppCompatActivity() {
var items = arrayOf(
"서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
"서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
"서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백",
"서울", "부산", "대구", "광주", "인천", "목포", "여수", "태백"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<View>(R.id.textView) as TextView
val grid = findViewById<View>(R.id.grid) as GridView
grid.setAdapter(
ArrayAdapter(this,R.layout.item_spinner, items)
//layout폴더에 있는 item_spinner.xml파일과 지역명 적인 배열 items 연결하는
//어댑터를 grid에 세팅
)
grid.setOnItemClickListener{parent, view, position,id ->
textView.text = items[position]
//textView에 선택된 지역명 표시되도록 연결
}
}
}
결과화면
'코딩 기록 > Kotlin' 카테고리의 다른 글
Kotlin 안드로이드 화면에 WebView이용해서 html 문서 연결하고 구현화면 버튼 클릭시 알림창 띄우기 (0) | 2022.02.07 |
---|---|
Kotlin 안드로이드 화면에 videoView이용해 동영상 삽입하기 (0) | 2022.02.07 |
ScrollView / TableLayout 이용하여 스크롤 가능한 안드로이드 화면 만들기 (0) | 2022.02.07 |
Kotlin / listview 이용해서 android화면에 스크롤 사용하는 긴 선택항목 만들기 (0) | 2022.02.07 |
kotlin 원하는 이미지파일 android 화면에서 배경으로 설정하는 방법 android:background (0) | 2022.02.07 |
Comments