๋ค์ด๊ฐ๊ธฐ ์ ..
[์ฐธ๊ณ ] XML
์๋ฆฌ๋จผํธ
- root element๋ ํ๋ ์ด์์ child element๋ฅผ ๊ฐ์ง
- ๊ฐ child element๋ ํ๋ ์ด์์ subchild element๋ฅผ ๊ฐ์ง ์ ์์
<root element>
<child element>
<subchild element>๋ฐ์ดํฐ</subchild element>
</child element>
</element>
- ๋ฐ์ดํฐ๊ฐ ์๋ ์๋ฆฌ๋จผํธ๋ empty element๋ผ๊ณ ํ๋ฉฐ, <element></element>๋๋ <element/>๋ก ํํํจ
- ์๋ฆฌ๋จผํธ๋ ์ฌ๋ฌ ๊ฐ์ ์์ฑ์ ์ง์ ํ ์ ์์ผ๋ฉฐ ๊ฐ ์์ฑ์ ์์ฑ๋ช ๊ณผ ์์ฑ๊ฐ์ผ๋ก ํํ
<element ์์ฑ๋ช
="์์ฑ๊ฐ">
์ฃผ์๋ฌธ
- xml๋ฌธ์์ ์ฃผ์๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํจ
<!-- ์ฃผ์๋ฌธ -->
์์ ฏ
ํ ์คํธ๋ทฐ
- View ํด๋์ค ๋ฐ๋ก ๋ค์์ ์์นํ๋ฉฐ ๋ค์ํ ์์ ฏ์ด ๊ทธ ํ์์ ์กด์ฌํจ
text ์์ฑ
- ํ ์คํธ๋ทฐ์ ๋ํ๋๋ ๋ฌธ์์ด์ ํํ
- “๋ฌธ์์ด” ํ์์ผ๋ก ๊ฐ์ ์ง์ ์ ๋ ฅํ๊ฑฐ๋ “@string/๋ณ์๋ช ” ํ์์ผ๋ก ์ง์ ํ ํ strings.xml ํ์ผ์ ์ง์ ํ ์ ์์
textColor ์์ฑ
- ๊ธ์์ ์์์ ์ง์
- background ์์ฑ์ฒ๋ผ ๊ฐ์ #RRGGBB๋ #AARRGGBB ํ์์ ์ฌ์ฉํจ
textSize ์์ฑ
- ๊ธ์์ ํฌ๊ธฐ๋ฅผ dp, px, in, mm, sp ๋จ์๋ก ์ง์
- ํ ์คํธ๊ฐ ์ ๋ ฅ๋๋ ์์ ฏ(ํ ์คํธ๋ทฐ, ์๋ํธํ ์คํธ)์ ์ฃผ๋ก sp๋ฅผ ์ฌ์ฉ. sp(๋๋ sip)๋ Scale-independent Pixels์ ์ฝ์๋ก ๋ฌธ์์ด ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋จ์
- (์์ ฏ์ ์ํฅ์ ์ฃผ์ง์๊ณ ๊ธ์๋ง ๋ณ๊ฒฝ๋๋ฏ๋ก sp ์ฌ์ฉ ๊ถ์ฅ)
typeface ์์ฑ
- ๊ธ์์ ๊ธ๊ผด์ ์ง์
- ๊ฐ์ผ๋ก sans, serif, monospace๋ฅผ ์ค์ ํ ์ ์๊ณ ๋ํดํธ๋ normal
textStyle ์์ฑ
- ๊ธ์์ ์คํ์ผ์ ์ง์
- ๊ฐ์ผ๋ก bold, italic, bold|italic์ ์ค์ ํ ์ ์๊ณ ๋ํดํธ๋ normal
singleLine ์์ฑ
- ๊ธ์ด ๊ธธ์ด ์ค์ด ๋์ด๊ฐ ๊ฒฝ์ฐ ๊ฐ์ ๋ก ํ ์ค๊น์ง๋ง ์ถ๋ ฅํ๊ณ ๋ฌธ์์ด์ ๋งจ ๋ค์ ‘...’๋ฅผ ํ์
- ๊ฐ์ผ๋ก true์ false๋ฅผ ์ค์ ํ ์ ์๊ณ ๋ํดํธ๋ false
maxLines ์์ฑ
- ํ ์คํธ๋ทฐ์ ์ต๋ ์ ๋ ฅ ๊ฐ๋ฅํ ์ค ์๋ฅผ ์ค์
minLines ์์ฑ
- ๋ฐ๋๋ก ์ต์ ์ค ์๋ฅผ ๋ฏธ๋ฆฌ ์ค์
ellipsize ์์ฑ
- ํ ์คํธ๋ทฐ์ maxLines ์์ฑ์ด 1์ด๊ฑฐ๋ ๋ฌธ์์ด์ด ๊ธธ์ด์ ๊ธ์๊ฐ ์๋ฆด ๋์ ์ค์
lines ์์ฑ
- ํ ์คํธ๋ทฐ ๋์ด ๊ณ ์ ํ๊ธฐ
maxLength
- ํ ์คํธ ์ ์ฒด ๊ธธ์ด ์ ํํ๊ธฐ
- maxLength๋ ํ ์คํธ์ ์ ์ฒด ๊ธธ์ด๋ฅผ ์ ํ, ์ค์ ํ ๊ฐ๋งํผ์ ๊ธ์๋ง ๋ณด์ด๊ณ ๋๋จธ์ง๋ ๋ณด์ด์ง ์๊ฒ ํจ
<TextView
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="10"
android:maxLength="10"
android:maxLines="1"
android:singleLine="false"
android:text="TextView ํ
์คํธ๋ทฐTextView ํ
์คํธ๋ทฐTextView ํ
์คํธ๋ทฐTextView ํ
์คํธ๋ทฐTextView ํ
์คํธ๋ทฐ"
android:textColor="#0000FF"
android:textSize="30sp"
android:textStyle="bold|italic"
android:typeface="serif"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
๋ง์ด ์ฌ์ฉ๋๋ View ํด๋์ค ๋๋ TextView ํด๋์ค์ XML ์์ฑ๊ณผ ๋ฉ์๋
์ค์ต (xml ์ฝ๋ ์๋ต)
package com.example.textview
import android.graphics.Color
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var tv1: TextView
var tv2: TextView
var tv3: TextView
tv1 = findViewById(R.id.textView1)
tv2 = findViewById(R.id.textView2)
tv3 = findViewById(R.id.textView3)
tv1.setText("์๋
ํ์ธ์?")
tv1.setTextColor(Color.RED)
tv2.setTextSize(30.0f)
tv2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC)
tv3.text = "๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ์์์ฐจ์นดํํํ๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ์์์ฐจ์นดํํํ๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ์์์ฐจ์นดํํํ"
tv3.setSingleLine()
}
}
์๋ํธํ ์คํธ, ๋ฒํผ
- ์ฌ์ฉ์์๊ฒ์ ์ด๋ค ๊ฐ์ ์ ๋ ฅ๋ฐ๊ธฐ ์ํ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์์ ฏ
- View์ TextView ํด๋์ค๋ฅผ ์์๋ฐ์ ⇒ ๊ฑฐ์ ๋น์ทํ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅ
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ํ
์คํธ๋ทฐ" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="๋ฒํผ" />
[์ฐธ๊ณ ]
TextView https://developer.android.com/reference/kotlin/android/widget/TextView
EditText https://developer.android.com/reference/kotlin/android/widget/EditTextrs
Button https://developer.android.com/reference/kotlin/android/widget/Button
์๋ํธํ ์คํธ
- ๊ฐ์ ์ ๋ ฅ๋ฐ์ ํ ํด๋น ๊ฐ์ Kotlin ์ฝ๋์์ ๊ฐ์ ธ์ ์ฌ์ฉํ๋ ์ฉ๋๋ก ๋ง์ด ์ฐ์
- ์ผ๋ฐ์ ์ธ ์๋ํธํ ์คํธ์ XML ์ฝ๋
<EditText
android:id="@+id/edittext1" />
hint ์์ฑ
- ํด๋ฆญํ๋ฉด ์ฌ๋ผ์ง๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์์ฑํ ์ ์์
inputType ์์ฑ
- inputType ์์ฑ์ ์ ๋ ฅ๋๋ ์ต์ ์ ๋ฐ๋ผ ํค๋ณด๋์ ๋ชจ์์ด ๋ฐ๋
- [number] : ์ซ์ ํ์
- [numberPassword] : ์ซ์๋ก๋ง ๊ตฌ์ฑ๋ ๋น๋ฐ๋ฒํธ ์ ๋ ฅ
- [phone] : ์ ํ๋ฒํธ ํ์
- [date] : ๋ ์ง ํ์
- [textUri] : URI ํ์์ ๋ฌธ์ ์ ๋ ฅ
- [textEmailAdress] : email ์ฃผ์ ํ์์ ๋ฌธ์ ์ ๋ ฅ
- [textPassword]: ๋น๋ฐ๋ฒํธ ์ ๋ ฅ
- [textVisiblePassword] : ๋น๋ฐ๋ฒํธ๋ฅผ ๋ฌธ์์ด ๊ทธ๋๋ก ํ์ํ๊ธฐ
๋ฒํผ์ ํด๋ฆญํ์ ๋ ๋์ํ๋ Kotlin ์ฝ๋๋ฅผ ์ธ ๋จ๊ณ๋ก ์์ฑ
- ์๋ํธํ ์คํธ ๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ด ๋ณ์์ ํด๋น id ๊ฐ์ ๋ฃ์ ํ์ ์ ๊ทผํจ
1. ์๋ํธํ ์คํธ ๋ณ์ ์ ์ธ
- var myEdit : EditText
2. ๋ณ์์ ์๋ํธํ ์คํธ ์์ ฏ ๋์
- myEdit = findViewById(R.id.edittext)
3. ์๋ํธํ ์คํธ์ ์ ๋ ฅ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ → ์ฃผ๋ก ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ๋๋ค์ ์์ ๋ฃ์
- var myStr : String = myEdit.getText().toString()
๋ฒํผ
- ์ผ๋ฐ์ ์ธ ๋ฒํผ์ XML ์ฝ๋
<Button
android:id="@+id/button1"
android:text="ํ์ธ" />
๋ฒํผ์ ํด๋ฆญํ์ ๋ ๋์ํ๋ Kotlin ์ฝ๋๋ฅผ ์ธ ๋จ๊ณ๋ก ์์ฑ
1. ๋ฒํผ ๋ณ์ ์ ์ธ
- var mybutton : Button
2. ๋ณ์์ ๋ฒํผ ์์ ฏ ๋์
- mybutton = findViewById(R.id.button1)
3. ๋ฒํผ์ ํด๋ฆญํ ๋ ๋์ํ๋ ๋๋ค์ ์ ์
- mybutton.setOnClickListener { // ๋์ ๋ด์ฉ์ ์ด ๋ถ๋ถ์ }
→ ์ธ ๋จ๊ณ๋ ๋๋ถ๋ถ์ ์์ ฏ(๋ผ๋์ค๋ฒํผ, ์ด๋ฏธ์ง๋ฒํผ, ์ฒดํฌ๋ฐ์ค, ํ ๊ธ๋ฒํผ ๋ฑ)์์ ๊ฑฐ์ ๋์ผํ๊ฒ ์ฌ์ฉ๋จ
์ค์ต1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ ํ๋ฒํธ" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="000-0000-0000"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์
๋ ฅ" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ทจ์" />
</LinearLayout>
</LinearLayout>
์ค์ต2 - ์ด๊ฐ๋จ ๊ณ์ฐ๊ธฐ ์ฑ ๋ง๋ค๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="์ซ์1"/>
<EditText
android:id="@+id/Edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="์ซ์2"/>
<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋ํ๊ธฐ"/>
<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋นผ๊ธฐ"/>
<Button
android:id="@+id/BtnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๊ณฒํ๊ธฐ"/>
<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="๋๋๊ธฐ"/>
<TextView
android:id="@+id/TextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#ff0000"
android:textSize="30sp"
android:text="๊ณ์ฐ ๊ฒฐ๊ณผ : "/>
</LinearLayout>
package com.example.simplecalapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
lateinit var edit1 : EditText
lateinit var edit2 : EditText
lateinit var btnAdd : Button
lateinit var btnSub : Button
lateinit var btnMul : Button
lateinit var btnDiv : Button
lateinit var textResult : TextView
lateinit var num1 : String
lateinit var num2 : String
var result : Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "์ด๊ฐ๋จ ๊ณ์ฐ๊ธฐ"
edit1 = findViewById(R.id.Edit1)
edit2 = findViewById(R.id.Edit2)
btnAdd = findViewById(R.id.BtnAdd)
btnSub = findViewById(R.id.BtnSub)
btnMul = findViewById(R.id.BtnMul)
btnDiv = findViewById(R.id.BtnDiv)
textResult = findViewById(R.id.TextResult)
btnAdd.setOnTouchListener { v, event ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) + Integer.parseInt(num2)
textResult.text = "๊ฒฐ๊ณผ ๊ณ์ฐ : " + result.toString()
false
}
btnSub.setOnTouchListener { v, event ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) - Integer.parseInt(num2)
textResult.text = "๊ฒฐ๊ณผ ๊ณ์ฐ : " + result.toString()
false
}
btnMul.setOnTouchListener { v, event ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) * Integer.parseInt(num2)
textResult.text = "๊ฒฐ๊ณผ ๊ณ์ฐ : " + result.toString()
false
}
btnDiv.setOnTouchListener { v, event ->
num1 = edit1.text.toString()
num2 = edit2.text.toString()
result = Integer.parseInt(num1) / Integer.parseInt(num2)
textResult.text = "๊ฒฐ๊ณผ ๊ณ์ฐ : " + result.toString()
false
}
}
}
์ปดํ์ด๋๋ฒํผ
CompoundButton ํด๋์ค
- Button ํด๋์ค์ ํ์ ํด๋์ค๋ก ์ฒดํฌ๋ฐ์ค, ๋ผ๋์ค๋ฒํผ, ์ค์์น, ํ ๊ธ๋ฒํผ์ ์์ ํด๋์ค
- ์ด ๋ค ๊ฐ์ง๋ ๊ณตํต์ ์ผ๋ก ์ฒดํฌ ๋๋ ์ธ์ฒดํฌ ์ํ๊ฐ ๋ ์ ์์
- ์ค์ ๋ก ๋น์ทํ ํํ๋ฅผ ๋ ์ง๋ง ์ฉ๋๋ ์กฐ๊ธ์ฉ ๋ค๋ฆ
์ฒดํฌ๋ฐ์ค
- ์ฒดํฌ๋ฐ์ค๋ ํด๋ฆญํ ๋๋ง๋ค ์ํ๊ฐ ์ฒดํฌ, ์ธ์ฒดํฌ๋ก ๋ฐ๋
- ์ฌ๋ฌ ๊ฐ์ ์ฒดํฌ๋ฐ์ค๊ฐ ์์ด๋ ์๋ก ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ค๋ ํน์ง์ด ์์ด ์ฌ๋ฌ ๊ฐ๋ฅผ ๋์์ ์ฒดํฌํ ์ ์์
์ฒดํฌ์ ์ธ์ฒดํฌ๊ฐ ๋ฐ๋์์ ๋ Kotlin ์ฝ๋์ ์ฒ๋ฆฌ ์ ์ฐจ
1. ์ฒดํฌ๋ฐ์ค ๋ณ์ ์ ์ธ
- var mycheck : CheckBox
2. ๋ณ์์ ์ฒดํฌ๋ฐ์ค ์์ ฏ ๋์
- mycheck = findViewById(R.id.android)
3. ์ฒดํฌ๋ฐ์ค๊ฐ ๋ณ๊ฒฝ๋ ๋ ๋์ํ๋ ๋๋ค์ ์ ์
- mycheck.setOnCheckedChangeListener { compoundButton, b -> // ๋์ ๋ด์ฉ์ ์ด ๋ถ๋ถ์ ์ฝ๋ฉ }
์ค์ต
[์ฐธ๊ณ ] ํ ์คํธ
- ํ๋ฉด ํ๋จ๋ถ์ ๋ฉ์์ง๋ฅผ ์ ์๋์ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ์
- Toast.makeText(Context context, String message, int duration).show()
package com.example.checkbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.Toast
class MainActivity : AppCompatActivity() {
lateinit var checkApple : CheckBox
lateinit var checkOrange : CheckBox
lateinit var checkBanana : CheckBox
var listener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
if(isChecked){
when(buttonView.id)
{
R.id.checkApple -> Toast.makeText(applicationContext,"์ฌ๊ณผ", Toast.LENGTH_SHORT).show()
R.id.checkOrange -> Toast.makeText(applicationContext, "์ค๋ ์ง", Toast.LENGTH_SHORT).show()
R.id.checkBanana -> Toast.makeText(applicationContext, "๋ฐ๋๋", Toast.LENGTH_SHORT).show()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkApple = findViewById(R.id.checkApple)
checkOrange = findViewById(R.id.checkOrange)
checkBanana = findViewById(R.id.checkBanana)
checkApple.setOnCheckedChangeListener(listener)
checkOrange.setOnCheckedChangeListener(listener)
checkBanana.setOnCheckedChangeListener(listener)
// checkApple.setOnCheckedChangeListener { buttonView, isChecked ->
// if(isChecked) {
// Toast.makeText(applicationContext, "์ฌ๊ณผ", Toast.LENGTH_SHORT).show()
// }
// }
//
// checkOrange.setOnCheckedChangeListener { buttonView, isChecked ->
// if(isChecked) {
// Toast.makeText(applicationContext, "์ค๋ ์ง", Toast.LENGTH_SHORT).show()
// }
// }
//
// checkBanana.setOnCheckedChangeListener { buttonView, isChecked ->
// if(isChecked) {
// Toast.makeText(applicationContext, "๋ฐ๋๋", Toast.LENGTH_SHORT).show()
// }
// }
}
}
์ค์์น, ํ ๊ธ๋ฒํผ
- ์ค์์น์ ํ ๊ธ๋ฒํผ์ ๋ชจ์๋ง ์กฐ๊ธ ๋ค๋ฅผ ๋ฟ ์ฉ๋๋ ๊ฑฐ์ ๋์ผํจ
- ์ค์์น์ ์ฃผ ์ฉ๋๋ ์จ/์คํ ์ํ ํ์
- XML ์์ฑ์ด๋ ๊ด๋ จ ๋ฉ์๋๋ ๋ชจ๋ ์ฒดํฌ๋ฐ์ค์ ๋์ผํ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅ
- checked ์์ฑ์ true์ false์ ๋ฐ๋ผ์ ์์๊ณผ ๊ธ์๊ฐ ๋ค๋ฅด๊ฒ ํํ
๋ผ๋์ค๋ฒํผ, ๋ผ๋์ค๊ทธ๋ฃน
๋ผ๋์ค๋ฒํผ
- XML ์์ฑ์ด๋ ๋ฉ์๋๊ฐ ์ฒดํฌ๋ฐ์ค์ ๊ฑฐ์ ๋์ผํ์ง๋ง ์ฉ๋๊ฐ ๋ค๋ฆ
- ์ฌ๋ฌ ๊ฐ ์ค ํ๋๋ง ์ ํํด์ผ ํ๋ ๊ฒฝ์ฐ์ ์ฌ์ฉ
» ๊ทธ๋ฌ๋ ๋ผ๋์ค๋ฒํผ๋ง ์ฌ๋ฌ ๊ฐ ๋์ดํ๋ฉด ํด๋ฆญํ๋ ๊ฒ๋ง๋ค ๋ชจ๋ ์ค๋ณต ์ ํ๋๋ฏ๋ก ๋ผ๋์ค๊ทธ๋ฃน๊ณผ ํจ๊ป ์ฌ์ฉํด์ผ ํจ
» ๊ฐ ๋ผ๋์ค๋ฒํผ์ id ์์ฑ์ด ๊ผญ ์์ด์ผ ํจ
» id ์์ฑ์ด ์์ผ๋ฉด ํด๋น ๋ผ๋์ค๋ฒํผ์ด ๊ณ์ ์ ํ๋ ๊ฒ์ผ๋ก ์ง์ ๋์ด ํด์ ๋์ง ์์
๋ผ๋์ค๊ทธ๋ฃน
- ViewGroup-LinearLayout์ ํ์ ํด๋์ค๋ก ์กด์ฌํจ
- TextView ํ์์ ์์ ฏ๋ค๊ณผ๋ ์ฑ๊ฒฉ์ด ์กฐ๊ธ ๋ค๋ฆ
- ๋ผ๋์ค๊ทธ๋ฃน์์ ๊ฐ๋ ์ฌ์ฉ๋๋ ๋ฉ์๋์ธ clearCheck()
» ํด๋น ๋ผ๋์ค๊ทธ๋ฃน ์์ ์ฒดํฌ๋ ๊ฒ์ ๋ชจ๋ ํด์ ํจ
์ค์ต
package com.example.radiogroup
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
class MainActivity : AppCompatActivity() {
lateinit var radioGroup : RadioGroup
lateinit var radioApple : RadioButton
lateinit var radioOrange : RadioButton
lateinit var radioBanana : RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
radioGroup = findViewById(R.id.radioGroup)
radioApple = findViewById(R.id.radioApple)
radioOrange = findViewById(R.id.radioOrange)
radioBanana = findViewById(R.id.radioBanana)
radioGroup.setOnCheckedChangeListener { group, checkedId ->
when(checkedId) {
R.id.radioApple -> Toast.makeText(applicationContext, "์ฌ๊ณผ", Toast.LENGTH_SHORT).show()
R.id.radioOrange -> Toast.makeText(applicationContext, "์ค๋ ์ง", Toast.LENGTH_SHORT).show()
R.id.radioBanana -> Toast.makeText(applicationContext, "๋ฐ๋๋", Toast.LENGTH_SHORT).show()
}
}
}
}
์ด๋ฏธ์ง๋ฒํผ
์ด๋ฏธ์ง๋ทฐ
- ๊ทธ๋ฆผ์ ์ถ๋ ฅํ๋ ์์ ฏ
- ๊ทธ๋ฆผ์ ๋ฃ๊ฑฐ๋ ํ๋ฉด์ ํ๋ คํ๊ฒ ๊ตฌ์ฑํ ๋ ์ฌ์ฉํจ
- ์ด๋ฏธ์ง๋ทฐ์ ๋ณด์ฌ์ค ๊ทธ๋ฆผ ํ์ผ์ ์ผ๋ฐ์ ์ผ๋ก ํ๋ก์ ํธ์ [res]-[drawable] ํด๋์ ์์ด์ผ ํจ
- ์ ๊ทผ์ XML์์ “@drawable/๊ทธ๋ฆผ ์์ด๋” ํ์์ผ๋ก ํจ
์ด๋ฏธ์ง๋ทฐ์ ์ด๋ฏธ์ง๋ฒํผ์ XML ์์ฑ
- src
- ์ด๋ฏธ์ง์ ๊ฒฝ๋ก๋ฅผ ๋ํ๋
- maxHeight/maxWidth
- ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ฅผ ์ง์ ํจ
- scaleType
- ์ด๋ฏธ์ง์ ํ๋/์ถ์ ๋ฐฉ์์ ์ง์
- scaleType์ ์์ฑ: matrix, fitXY, fitStart, fitEnd, center ๋ฑ ์ง์ ํ ๊ฐ์ ๋ฐ๋ผ ์ด๋ฏธ์ง๋ฅผ ํ๋/์ถ์ํ๋ ๋ฐฉ์์ด ๊ฒฐ์ ๋จ
- matrix: ์ค์ ์ด๋ฏธ์ง๋ฅผ ์ข์ธก ์๋จ๋ถํฐ ์ด๋ฏธ์ง๋ฒํผ์ ํฌ๊ธฐ๋งํผ ๋ณด์ฌ์ค
- fitXY: ์ํ์ข์ฐ๋ฅผ ํฌ๊ธฐ๋ฅผ ๋๋ ค์ ๋ง์ถฐ์ ๋ณด์ฌ์ค
- fitStart: ์ข์ธก ์๋จ ๋ถํฐ ์์ํด ์ด๋ฏธ์ง ๋น์จ์ ๋ง๊ฒ ์กฐ์ ํด์ ์์ ฏ ์์ ๋ฃ์ด์ค
- fitEnd: ์ฐ์ธก ํ๋จ๋ถํฐ ์์ํด ์ด๋ฏธ์ง ๋น์จ์ ๋ง๊ฒ ์กฐ์ ํด์ ์์ ฏ ์์ ๋ฃ์ด์ค
- cemter: ์ค์ ์ด๋ฏธ์ง ์ฌ์ด์ฆ ๊ทธ๋๋ก ์ ์ค์์ ์์น ์ํด(์์ ฏ๋ณด๋ค ์ด๋ฏธ์ง ํด ๊ฒฝ์ฐ ์๋ ค ๋ณด์ผ ์ ์์)
์ด๋ฏธ์ง๋ทฐ์ ์ด๋ฏธ์ง๋ฒํผ์ XML ์์ฑ
- ์ฌ์ฉํ๋ ค๋ ๊ทธ๋ฆผ ํ์ผ์ด [res]-[drawable] ํด๋์ ์์ด์ผ ํจ
- ํ์ผ ํฌ๋งท
- png, jpg, gif๋ฅผ ์ง์ํ์ง๋ง png๋ jpg๋ฅผ ๊ถ์ฅํจ
- [res]-[mipmap]
- ๊ฐ์ ์ด๋ฆ์ ์ฑ ์์ด์ฝ ์ด๋ฏธ์ง(ic_launcher.png)๊ฐ ๋ํดํธ๋ก ๋ค์ด ์์
- ๊ฐ๊ฐ์ ๊ฐ์ ์ด๋ฏธ์ง์ด์ง๋ง ํด์๋๊ฐ mdpi(48×48), hdpi(72×72), xhdpi(96×96), xxhdpi(144×144), xxxhdpi(192× 192) ๋ฑ์ผ๋ก ๋ค๋ฆ
- XML ํ์ผ์์๋ @mipmap/ic_launcher๋ก, Kotlin ์ฝ๋์์๋ R.mipmap.ic_launcher๋ก ์ฌ์ฉ๋จ
- ์ด ์ฑ ์์ด์ฝ์ธ ic_launcher๋ฅผ ํ๋ฉด์ ์ถ๋ ฅํ๋ฉด ์๋๋ก์ด๋ ์ด์์ฒด์ ๊ฐ ํ์ฌ ์๋๋ก์ด๋ํฐ์ ํด์๋์ ์ ์ ํ ์ด๋ฏธ์ง๋ฅผ ์์์ ์ ํํจ
์์
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/q10" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/q10" />
</LinearLayout>
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="fitCenter" />
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="fitXY" />
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="fitStart" />
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="fitEnd" />
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="center" />
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:background="#ffff00"
android:src="@drawable/q10"
android:scaleType="matrix" />
</LinearLayout>
์ค์ต - ๋ฐ๋ ค๋๋ฌผ ์ฌ์ง ๋ณด๊ธฐ ์ฑ ๋ง๋ค๊ธฐ
์ก์ ๋ฐ(Action bar)๊ฐ ๋ณด์ด์ง ์๋ ๊ฒฝ์ฐ ์ ๋ค๋น๊ฒ์ด์ ์ค์ต ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์๊ธฐ๋๋ฌธ์ themes.xml์ ์์ฑ ์ค <style name = "Base.Theme.ProjectBMICalculator" parent="Theme.Material3.Light.NoActionBar">์ NoActionBar๋ฅผ ์ง์์ค๋๋ค.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ ํ์ ์์ํ์๊ฒ ์ต๋๊น?"
android:textSize="20sp" />
<CheckBox
android:id="@+id/ChkAgree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์์ํจ" />
<TextView
android:id="@+id/Text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ข์ํ๋ ๋ฐ๋ ค๋๋ฌผ์?"
android:textSize="20sp"
android:visibility="invisible" />
<RadioGroup
android:id="@+id/Rgroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
<RadioButton
android:id="@+id/RdoDog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="๊ฐ์์ง" />
<RadioButton
android:id="@+id/RdoCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="๊ณ ์์ด" />
<RadioButton
android:id="@+id/RdoRabbit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ํ ๋ผ" />
</RadioGroup>
<Button
android:id="@+id/BtnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ ํ์๋ฃ"
android:visibility="invisible" />
<ImageView
android:id="@+id/ImgPet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
package com.example.petimageapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
lateinit var text1 : TextView
lateinit var text2 : TextView
lateinit var chkAgree : CheckBox
lateinit var rGroup1 : RadioGroup
lateinit var rdoDog : RadioButton
lateinit var rdoCat : RadioButton
lateinit var rdoRabbit : RadioButton
lateinit var btnOk : Button
lateinit var imgPet : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "๋ฐ๋ ค๋๋ฌผ ์ฌ์ง ๋ณด๊ธฐ"
text1 = findViewById(R.id.Text1)
chkAgree = findViewById(R.id.ChkAgree)
text2 = findViewById(R.id.Text2)
rGroup1 = findViewById(R.id.Rgroup1)
rdoDog = findViewById(R.id.RdoDog)
rdoCat = findViewById(R.id.RdoCat)
rdoRabbit = findViewById(R.id.RdoRabbit)
btnOk = findViewById(R.id.BtnOk)
imgPet = findViewById(R.id.ImgPet)
chkAgree.setOnCheckedChangeListener { buttonView, isChecked ->
if(chkAgree.isChecked == true) {
text2.visibility = android.view.View.VISIBLE
rGroup1.visibility = android.view.View.VISIBLE
btnOk.visibility = android.view.View.VISIBLE
imgPet.visibility = android.view.View.VISIBLE
}
else {
text2.visibility = android.view.View.INVISIBLE
rGroup1.visibility = android.view.View.INVISIBLE
btnOk.visibility = android.view.View.INVISIBLE
imgPet.visibility = android.view.View.INVISIBLE
}
}
btnOk.setOnClickListener {
when(rGroup1.checkedRadioButtonId) {
R.id.RdoDog -> imgPet.setImageResource(R.drawable.dog)
R.id.RdoCat -> imgPet.setImageResource(R.drawable.cat)
R.id.RdoRabbit -> imgPet.setImageResource(R.drawable.rabbit)
else -> Toast.makeText(applicationContext, "๋๋ฌผ ๋จผ์ ์ ํํ์ธ์", Toast.LENGTH_SHORT).show()
}
}
}
}