반응형

[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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.cookandroid.project9_1
 
import android.content.Context
import android.content.res.Resources
import android.graphics.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import androidx.core.graphics.scale
import com.cookandroid.project9_1.MainActivity.Companion.curShape
 
class MainActivity : AppCompatActivity() {
 
    companion object{
        const val LINE = 1
        const val CIRCLE = 2
        const val RECT = 3
        const val IMAGE = 4
        var curColor = Color.RED
        var curShape = LINE
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(MyGraphicView(this))
        title = "간단 그림판"
 
 
    }
    private class MyGraphicView(context: Context) : View(context){
        var startX = -1
        var startY = -1
        var stopX = -1
        var stopY = -1
 
        override fun onTouchEvent(event: MotionEvent?): Boolean {
            when(event!!.action){
                MotionEvent.ACTION_DOWN ->{
                    startX = event.x.toInt()
                    startY = event.y.toInt()
                }
                MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
                    stopX = event.x.toInt()
                    stopY = event.y.toInt()
                    this.invalidate()
                }
            }
            return true
        }
 
        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
            val paint = Paint()
            paint.isAntiAlias = true
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 5f
            paint.color = curColor
 
            when(curShape){
                LINE -> {
                    canvas!!.drawLine(startX.toFloat(), startY.toFloat(),
                        stopX.toFloat(), stopY.toFloat(), paint)
                }
                CIRCLE -> {
                    var radius = Math.sqrt(Math.pow((stopX - startX).toDouble() , 2.0) + Math.pow((stopY - startY).toDouble(), 2.0))
                    canvas!!.drawCircle(startX.toFloat(), startY.toFloat(), radius.toFloat(), paint)
                }
                RECT -> {
                    canvas!!.drawRect(RectF(startX.toFloat(), startY.toFloat(),
                        stopX.toFloat(), stopY.toFloat()
                    ), paint)
                }
                IMAGE -> {
                    var picture = BitmapFactory.decodeResource(resources, R.drawable.jeju14)
                    picture.scale(500, 300)
                    canvas!!.drawBitmap(picture, stopX.toFloat(), stopY.toFloat(), paint)
                }
 
            }
        }
    }
 
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu1, menu)
 
        return super.onCreateOptionsMenu(menu)
    }
 
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.itemLine ->{
                curShape = LINE
            }
            R.id.itemCircle ->{
                curShape = CIRCLE
            }
            R.id.itemRect -> {
                curShape = RECT
            }
            R.id.itemImage -> {
                curShape = IMAGE
            }
            R.id.itemRed -> {
                curColor = Color.RED
            }
 
            R.id.itemBlue -> {
                curColor = Color.BLUE
            }
 
            R.id.itemGreen -> {
                curColor = Color.GREEN
 
            }
        }
        return super.onOptionsItemSelected(item)
    }
 
}
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 
 
    <item
        android:id="@+id/itemLine"
        android:title="선 그리기">
    </item>
 
    <item
        android:id="@+id/itemCircle"
        android:title="원 그리기">
    </item>
 
 
    <item
        android:id="@+id/itemRect"
        android:title="사각형 그리기">
    </item>
 
    <item
        android:id="@+id/itemImage"
        android:title="이미지 그리기">
    </item>
 
    <item
        android:id="@+id/itemColor"
        android:title="색상 변경>>">
        <menu>
            <item
                android:id="@+id/itemRed"
                android:title="빨강색">
            </item>
            <item
                android:id="@+id/itemBlue"
                android:title="파랑색">
            </item>
            <item
                android:id="@+id/itemGreen"
                android:title="초록색">
            </item>
 
 
        </menu>
 
    </item>
</menu>
cs
반응형
반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

[AndroidManifest.xml]

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 추가

 

<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
package com.cookandroid.project8_2
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.cookandroid.project8_2.myPictureView
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
 
class MainActivity : AppCompatActivity() {
    var imageFiles : Array<File>? = null
    lateinit var imageFname : String
    var curNum  = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "간단한 이미지 뷰어"
        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), Context.MODE_PRIVATE)
 
        imageFiles = File(Environment.getExternalStorageDirectory().absolutePath + "/Pictures").listFiles()
        imageFname = imageFiles!![0].toString()
        myPictureView.imagePath = imageFname
        tv.setText("1/${imageFiles!!.size - 1}")
 
        btnPrev.setOnClickListener {
            if(curNum <= 0){
                curNum = imageFiles!!.size - 2
            }else{
                curNum--
            }
            tv.setText("${curNum + 1}/${imageFiles!!.size -1}")
            imageFname = imageFiles!![curNum].toString()
            myPictureView.imagePath = imageFname
            myPictureView.invalidate()
        }
        btnNext.setOnClickListener {
            if(curNum == imageFiles!!.size -2){
                curNum = 0
            }else{
                curNum++
            }
            tv.setText("${curNum + 1}/${imageFiles!!.size -1}")
            imageFname = imageFiles!![curNum].toString()
            myPictureView.imagePath = imageFname
            myPictureView.invalidate()
        }
 
 
    }
}
cs

<myPictureView>

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
package com.cookandroid.project8_2
 
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
 
class myPictureView(context: Context, attrs: AttributeSet?): View(context, attrs) {
 
    var imagePath : String? = null
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        try{
            if(imagePath != null){
                var bitmap = BitmapFactory.decodeFile(imagePath)
                canvas!!.scale(2f, 2f, 0f, 0f)
                canvas!!.drawBitmap(bitmap!!, 0f, 0f, null)
                bitmap!!.recycle()
 
            }
        }catch(e :Exception){
 
        }
    }
}
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
<?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">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnPrev"
            android:text="이전 그림"
            android:layout_weight="1"/>
        <TextView
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0/0"
            android:id="@+id/tv"/>
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnNext"
            android:text="다음 그림"
            android:layout_weight="1"/>
 
    </LinearLayout>
    <com.cookandroid.project8_2.myPictureView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myPictureView"/>
 
 
</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
package com.cookandroid.project8_1
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
import java.nio.charset.Charset
import java.util.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "간단 일기장"
 
        var cal = Calendar.getInstance()
        var cYear = cal.get(Calendar.YEAR)
        var cMonth = cal.get(Calendar.MONTH)
        var cDay = cal.get(Calendar.DAY_OF_MONTH)
        var fileName  = "${cYear}_${cMonth}_${cDay}.txt"
        var str = readDiary(fileName)
        edtDiary.setText(str)
        btnWrite.isEnabled = true
 
 
 
        datePicker1.init(cYear, cMonth, cDay){view, year, monthOfYear, dayOfMonth ->
            fileName = year.toString() + "_" + monthOfYear.toShort() + "_" + dayOfMonth.toString() + ".txt"
            var str = readDiary(fileName!!)
            edtDiary.setText(str)
            btnWrite.isEnabled = true
        }
 
        btnWrite.setOnClickListener {
            var outFs = openFileOutput(fileName, Context.MODE_PRIVATE)
            var str = edtDiary.text.toString()
            outFs.write(str.toByteArray())
            outFs.close()
            Toast.makeText(applicationContext, "${fileName} 이 저장됨", Toast.LENGTH_SHORT).show()
        }
 
    }
 
    fun readDiary(fName : String) : String?{
        var diaryStr : String? = null
        try{
            var inFs = openFileInput(fName)
            var txt = ByteArray(inFs.available())
            inFs.read(txt)
            inFs.close()
            diaryStr = txt.toString(Charsets.UTF_8).trim()
            btnWrite.text = "수정하기"
 
        }catch(e : Exception){
            edtDiary.hint = "일기 없음"
            btnWrite.text = "새로 저장"
        }
        return diaryStr
 
    }
}
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
<?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">
 
    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"
        android:id="@+id/datePicker1"/>
    <EditText
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="8"
        android:id="@+id/edtDiary"
        android:background="#00ff00"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="새로 저장"
        android:layout_margin="5dp"
        android:enabled="false"
        android:id="@+id/btnWrite"/>
 
</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
package com.example.myapplication
 
import android.app.TabActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.MultiAutoCompleteTextView
import kotlinx.android.synthetic.main.activity_main.*
 
@Suppress("deprecation")
class MainActivity : TabActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        var tabHost = this.tabHost
 
        var tabSpecDog = tabHost.newTabSpec("DOG").setIndicator("강아지")
        tabSpecDog.setContent(R.id.tabDog)
        tabHost.addTab(tabSpecDog)
 
        var tabSpecCat = tabHost.newTabSpec("CAT").setIndicator("고양이")
        tabSpecCat.setContent(R.id.tabCat)
        tabHost.addTab(tabSpecCat)
 
        var tabSpecRabbit = tabHost.newTabSpec("RABBIT").setIndicator("토끼")
        tabSpecRabbit.setContent(R.id.tabRabbit)
        tabHost.addTab(tabSpecRabbit)
 
        var tabSpecHorse = tabHost.newTabSpec("HORSE").setIndicator("말")
        tabSpecHorse.setContent(R.id.tabHorse)
        tabHost.addTab(tabSpecHorse)
 
        tabHost.currentTab = 0
    }
}
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
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
 
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@android:id/tabcontent">
 
            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/dog"
                android:id="@+id/tabDog"/>
 
            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/cat"
                android:id="@+id/tabCat"/>
 
            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/rabbit"
                android:id="@+id/tabRabbit"/>
 
            <ImageView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/horse"
                android:id="@+id/tabHorse"/>
        </FrameLayout>
 
        <TabWidget
            android:background="#ffff00"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"/>
 
    </LinearLayout>
 
 
</TabHost>
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
package com.example.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.MultiAutoCompleteTextView
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "사진 보기"
       viewFlipper.flipInterval = 1000
        btnStart.setOnClickListener {
            viewFlipper.startFlipping()
        }
        btnStop.setOnClickListener {
            viewFlipper.stopFlipping()
        }
 
    }
}
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
<?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">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnStart"
            android:text="사진보기 시작"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnStop"
            android:text="사진보기 정지"/>
    </LinearLayout>
 
    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewFlipper">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/cat"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/dog"/>
 
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/horse"/>
 
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/rabbit"/>
 
    </ViewFlipper>
</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
package com.cookandroid.project6_1
 
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "시간 예약(수정)"
        radioG.visibility = View.INVISIBLE
 
        timePicker1.visibility = View.INVISIBLE
        datePicker1.visibility = View.INVISIBLE
        rdoCal.setOnClickListener {
            datePicker1.visibility = View.VISIBLE
            timePicker1.visibility = View.INVISIBLE
        }
        rdoTime.setOnClickListener {
            datePicker1.visibility = View.INVISIBLE
            timePicker1.visibility = View.VISIBLE
        }
        chronometer1.setOnClickListener {
            chronometer1.start()
            chronometer1.setTextColor(Color.RED)
            radioG.visibility = View.VISIBLE
        }
 
        tvYear.setOnLongClickListener {
            chronometer1.stop()
            radioG.visibility = View.INVISIBLE
            radioG.clearCheck()
            timePicker1.visibility = View.INVISIBLE
            datePicker1.visibility = View.INVISIBLE
            chronometer1.setTextColor(Color.BLUE)
            tvYear.text = datePicker1.year.toString()
            tvMonth.text = datePicker1.month.toString()
            tvDay.text = datePicker1.dayOfMonth.toString()
            tvHour.text = timePicker1.currentHour.toString()
            tvMinute.text  = timePicker1.currentMinute.toString()
            true
        }
 
    }
}
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?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">
 
    <Chronometer
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:format="예약에 걸린 시간 %s"
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#0000FF"
        android:id="@+id/chronometer1"/>
 
    <RadioGroup
        android:id="@+id/radioG"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="날짜 설정(캘린더뷰)"
            android:id="@+id/rdoCal"/>
        <RadioButton
            android:checked="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="시간 설정"
            android:id="@+id/rdoTime"/>
    </RadioGroup>
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <DatePicker
                android:datePickerMode="spinner"
                android:id="@+id/datePicker1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            <TimePicker
                android:id="@+id/timePicker1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:timePickerMode="spinner"/>
        </FrameLayout>
    </LinearLayout>
 
    <LinearLayout
        android:background="#CCCCCC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0000"
            android:id="@+id/tvYear"/>
        <TextView
            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="00"
            android:id="@+id/tvMonth"/>
        <TextView
            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="00"
            android:id="@+id/tvDay"/>
        <TextView
            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="00"
            android:id="@+id/tvHour"/>
        <TextView
            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="00"
            android:id="@+id/tvMinute"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="분 예약됨"/>
    </LinearLayout>
 
</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
package com.cookandroid.project7_3
 
import android.content.res.Resources
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.dialog1.view.*
import kotlinx.android.synthetic.main.toast1.*
import kotlinx.android.synthetic.main.toast1.view.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "사용자 정보 입력"
 
        button1.setOnClickListener {
            var dialogView = View.inflate(this@MainActivity, R.layout.dialog1, null)
            var dlg = AlertDialog.Builder(this@MainActivity)
            dialogView.dlgEdt1.text = tvName.text
            dialogView.dlgEdt2.text = tvEmail.text
            dlg.setTitle("사용자 정보 입력")
            dlg.setIcon(R.drawable.ic_menu_allfriends)
            dlg.setView(dialogView)
            dlg.setPositiveButton("확인"){dialog, which->
                tvName.text = dialogView.dlgEdt1.text
                tvEmail.text = dialogView.dlgEdt2.text
            }
            dlg.setNegativeButton("취소"){dialog, which->
                var toast = Toast(this@MainActivity)
                var toastView = View.inflate(this@MainActivity, R.layout.toast1, null)
                toastView.toastText1.text = "취소했습니다"
                toast.view = toastView
                var xOffset = (Math.random() * Resources.getSystem().displayMetrics.widthPixels).toInt()
                var yOffset = (Math.random() * Resources.getSystem().displayMetrics.heightPixels).toInt()
                toast.setGravity(Gravity.TOP or Gravity.LEFT,xOffset, yOffset)
                toast.show()
 
            }
            dlg.show()
        }
    }
}
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
<?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:gravity="center_horizontal">
 
    <EditText
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="사용자 이름"/>
    <EditText
        android:id="@+id/tvEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:hint="여기를 클릭"/>
 
 
 
 
 
</LinearLayout>
cs

 

<dialog1.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
<?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">
 
    <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:id="@+id/dlgEdt1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이메일"/>
 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dlgEdt2"/>
 
</LinearLayout>
cs

 

 

<toast1.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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#ff0000"
    android:gravity="center">
 
 
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_star_big_on"/>
 
   <TextView
       android:id="@+id/toastText1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TextView"
       android:textSize="20dp"/>
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_star_big_on"/>
 
 
</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
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 = "연습문제 10-5"
        btnNewActivity.setOnClickListener {
            startActivity(Intent(applicationContext, Second::class.java))
        }
    }
 
}
cs

<Second>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.cookandroid.myapplication
 
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
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()
        }
        btnNewThirdActivity.setOnClickListener {
            startActivity(Intent(applicationContext, Third::class.java))
 
        }
    }
}
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
<?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">
    <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
14
15
16
17
18
19
20
21
22
23
<?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:background="#00FF00">
 
    <Button
        android:id="@+id/btnNewThirdActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="새 화면 열기"
        android:background="#00F000"/>
 
    <Button
        android:id="@+id/btnReturn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="돌아가기"
        android:background="#00F000"/>
 
</LinearLayout>
cs

<activity_third.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="돌아가기"
        android:background="#0000F0"/>
 
</LinearLayout>
cs

 

반응형

+ Recent posts