반응형
ListDialog
설명
- 리스트뷰를 표시할 수 있는 다이얼로그
코드
res/layout/activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/basicListDialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open BasicListDialog" />
<Button
android:id="@+id/customListDialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open CustomListDialog" />
</LinearLayout>
res/layout/custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/codeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / " />
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
MainActivity
class MainActivity : AppCompatActivity() {
private val data1 = arrayOf("항목1", "항목2", "항목3")
private val data2 = listOf(
mutableMapOf("code" to "AA01", "title" to "항목1"),
mutableMapOf("code" to "AA02", "title" to "항목2"),
mutableMapOf("code" to "AA03", "title" to "항목3")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val basicListDialogButton = findViewById<Button>(R.id.basicListDialogButton)
val customListDialogButton = findViewById<Button>(R.id.customListDialogButton)
basicListDialogButton.setOnClickListener {
val listener = DialogInterface.OnClickListener { dialog, which ->
Toast.makeText(this, data1[which], Toast.LENGTH_SHORT).show()
}
val builder = AlertDialog.Builder(this)
builder.setTitle("리스트 다이얼로그")
builder.setNegativeButton("취소", null)
builder.setItems(data1, listener)
builder.show()
}
customListDialogButton.setOnClickListener {
val keys = arrayOf("code", "title")
val ids = intArrayOf(R.id.codeTextView, R.id.titleTextView)
val adapter = SimpleAdapter(this, data2, R.layout.custom_dialog, keys, ids)
val listener = DialogInterface.OnClickListener { dialog, which ->
Toast.makeText(this, data2[which]["code"], Toast.LENGTH_SHORT).show()
}
val builder = AlertDialog.Builder(this)
builder.setTitle("커스텀 다이얼로그")
builder.setAdapter(adapter, listener)
builder.setNegativeButton("취소", null)
builder.show()
}
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] Fragment (0) | 2021.02.09 |
---|---|
[Android] DatePicker (0) | 2021.02.09 |
[Android] ProgressDialog (0) | 2021.02.09 |
[Android] AlertDialog (0) | 2021.02.09 |
[Android] ActionBar (0) | 2021.02.09 |