반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.cookandroid.myapplication
 
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "MainActivity"
        btnNewActivity.setOnClickListener {
            if(btnSecond.isChecked) startActivity(Intent(applicationContext, Second::class.java))
            if(btnThird.isChecked) startActivity(Intent(applicationContext, Third::class.java))
        }
    }
 
}
cs

<Second>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*
 
class Second : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        title = "Second"
        btnReturn.setOnClickListener {
            finish()
        }
    }
}
cs

<Third>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*
 
class Third : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_third)
        title = "Third"
        btnReturn.setOnClickListener {
            finish()
        }
    }
}
cs


 

<activity_main.xml>

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
29
30
31
<?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="vertical">
 
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/btnSecond"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second 액티비티"/>
        <RadioButton
            android:id="@+id/btnThird"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third 액티비티"/>
 
 
    </RadioGroup>
 
    <Button
        android:id="@+id/btnNewActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="새 화면 열기"/>
 
 
</LinearLayout>
cs

<activity_second.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:background="#00FF00">
 
    <Button
        android:id="@+id/btnReturn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="돌아가기"/>
 
</LinearLayout>
cs

<activity_third.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:background="#0000FF">
 
    <Button
        android:id="@+id/btnReturn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="돌아가기"/>
 
</LinearLayout>
cs
반응형
반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "초간단 계산기(수정)"
 
        btnAdd.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() + edit2.text.toString().toFloat()}"
            }
        }
        btnSub.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() - edit2.text.toString().toFloat()}"
            }
        }
        btnMul.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() * edit2.text.toString().toFloat()}"
            }
        }
        btnDiv.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else if(edit2.text.toString().toFloat() == 0.0f){
                Toast.makeText(applicationContext, "0으로 나눌 수 없습니다.", Toast.LENGTH_SHORT).show()
            } else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() / edit2.text.toString().toFloat()}"
            }
        }
 
        btnR.setOnClickListener {
            if(edit1.text.isEmpty() || edit2.text.isEmpty()){
                Toast.makeText(applicationContext, "값을 입력해주세요", Toast.LENGTH_SHORT).show()
            }else if(edit2.text.toString().toFloat() == 0.0f){
                Toast.makeText(applicationContext, "0으로 나눌 수 없습니다.", Toast.LENGTH_SHORT).show()
            } else{
                result.text = "계산 결과: ${edit1.text.toString().toFloat() % edit2.text.toString().toFloat()}"
            }
        }
    }
}
cs

<activity_main.xml>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?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="vertical">
 
    <EditText
        android:id="@+id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="숫자1"/>
 
    <EditText
        android:id="@+id/edit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="숫자2"/>
 
    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="더하기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="빼기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnMul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="곱하기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나누기"
        android:layout_margin="10dp"/>
 
    <Button
        android:id="@+id/btnR"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="나머지값"
        android:layout_margin="10dp"/>
 
    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="계산 결과: "
        android:textSize="30dp"
        android:textColor="#FF0000"/>
 
 
 
</LinearLayout>
cs
반응형
반응형

[TIP]

RadioGroup 메소드중에 clearCheck()을 호출하면 RadioGroup 안에 모든 Button의 체크 상태가 해제됨  

 

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.cookandroid.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.isNotEmpty
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "애완동물 사진 보기"
        ChkAgree.setOnCheckedChangeListener { compoundButton, b ->
            if(b){
                Text2.visibility = android.view.View.VISIBLE
                RdoDog.visibility = android.view.View.VISIBLE
                RdoCat.visibility = android.view.View.VISIBLE
                RdoRabbit.visibility = android.view.View.VISIBLE
                BtnFinish.visibility = android.view.View.VISIBLE
                BtnReset.visibility = android.view.View.VISIBLE
            }
            else{
                Rgroup1.clearCheck()
                Text2.visibility = android.view.View.INVISIBLE
                RdoDog.visibility = android.view.View.INVISIBLE
                RdoCat.visibility = android.view.View.INVISIBLE
                RdoRabbit.visibility = android.view.View.INVISIBLE
                BtnFinish.visibility = android.view.View.INVISIBLE
                BtnReset.visibility = android.view.View.INVISIBLE
                ImgPet.visibility = android.view.View.INVISIBLE
            }
        }
        Rgroup1.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.RdoDog -> {
                    ImgPet.setImageResource(R.drawable.dog);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
                R.id.RdoCat -> {
                    ImgPet.setImageResource(R.drawable.cat);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
                R.id.RdoRabbit-> {
                    ImgPet.setImageResource(R.drawable.rabbit);
                    ImgPet.visibility = android.view.View.VISIBLE
                }
            }
        }
        BtnFinish.setOnClickListener {
            finish()
        }
        BtnReset.setOnClickListener {
            Rgroup1.clearCheck()
            Text2.visibility = android.view.View.INVISIBLE
            RdoDog.visibility = android.view.View.INVISIBLE
            RdoCat.visibility = android.view.View.INVISIBLE
            RdoRabbit.visibility = android.view.View.INVISIBLE
            BtnFinish.visibility = android.view.View.INVISIBLE
            BtnReset.visibility = android.view.View.INVISIBLE
            ImgPet.visibility = android.view.View.INVISIBLE
            ChkAgree.isChecked = false
        }
    }
}
cs

<activity_main.xml>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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="vertical"
    android:padding="20dp">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Text1"
        android:text="선택을 시작하겠습니까?"/>
    <Switch
        android:id="@+id/ChkAgree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="시작함"/>
    <TextView
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Text2"
        android:text="좋아하는 애완동물은?"/>
    <RadioGroup
        android:id="@+id/Rgroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoDog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="강아지"/>
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoCat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="고양이"/>
        <RadioButton
            android:visibility="invisible"
            android:id="@+id/RdoRabbit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="토끼"/>
    </RadioGroup>
 
    <ImageView
        android:id="@+id/ImgPet"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"
        android:adjustViewBounds="true"/>
    <Button
        android:visibility="invisible"
        android:id="@+id/BtnFinish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="종료"/>
    <Button
        android:visibility="invisible"
        android:id="@+id/BtnReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="처음으로"/>
 
 
</LinearLayout>
cs

 

반응형
반응형

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.cookandroid.project7_2
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "배경색 바꾸기(컨텍스트 메뉴)"
 
        registerForContextMenu(button1)
        registerForContextMenu(button2)
 
    }
 
    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
 
        if(v === button1){
            menu!!.setHeaderTitle("배경석 변경")
            menu!!.add(0, 1, 0, "배경색(빨강)")
            menu!!.add(0, 2, 0, "배경색(초록)")
            menu!!.add(0, 3, 0, "배경색(파랑)")
        }
        if(v === button2){
            menu!!.add(0, 4, 0, "버튼 45도 회전")
            menu!!.add(0, 5, 0, "버튼 2개 확대")
        }
    }
 
    override fun onContextItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            1 -> {
                baseLayout.setBackgroundColor(Color.RED)
            }
 
            2 -> {
                baseLayout.setBackgroundColor(Color.GREEN)
            }
 
            3 -> {
                baseLayout.setBackgroundColor(Color.BLUE)
            }
            4 ->{
                button2.rotation += 45f
            }
            5 ->{
                button2.scaleX += 2f
                button2.scaleY += 2f
            }
        }
        return super.onContextItemSelected(item)
    }
 
 
}
cs

 

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="배경색 변경"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="버튼 변경"/>
 
</LinearLayout>
cs

 

반응형
반응형

[TIP]

1. 이미지뷰의 이미지를 변경하려면 setImageResource() 메소드를 사용

2. 메뉴 XML에서 여러 항목을 묶으려면 <group>...</group>을 사용한다

 

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

<MainActivity>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.cookandroid.project7_1
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.SubMenu
import androidx.core.text.isDigitsOnly
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "애완동물 사진 고르기"
    }
 
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu1, menu)
        return super.onCreateOptionsMenu(menu)
    }
 
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        item.isChecked = !item.isChecked
        when(item.itemId){
            R.id.rotate ->{
                if(edit.text.isNotEmpty() && edit.text.isDigitsOnly()) img.rotation += edit.text.toString().toFloat()
            }
            R.id.itemDog ->{
                img.setImageResource(R.drawable.dog)
            }
 
            R.id.itemCat ->{
                img.setImageResource(R.drawable.cat)
            }
            R.id.itemRabbit ->{
                img.setImageResource(R.drawable.rabbit)
            }
        }
        return super.onOptionsItemSelected(item)
    }
 
 
}
cs

<activity_main.xml>

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
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/edit"
        android:text="각도 입력"
        android:textSize="20dp" />
 
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text"
        android:hint="숫자"
        android:textSize="20dp"/>
 
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"/>
 
 
 
 
</RelativeLayout>
cs

 

<menu1.xml>

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
29
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item
        android:id="@+id/rotate"
        android:title="그림 회전">
 
    </item>
 
    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/itemDog"
            android:checked="true"
            android:title="강아지">
        </item>
 
        <item
            android:id="@+id/itemCat"
            android:title="고양이">
        </item>
 
        <item
            android:id="@+id/itemRabbit"
            android:title="토끼">
        </item>
        </group>
 
</menu>
cs
반응형

+ Recent posts