Android Application

Kotlin 기본 위젯 다루기

작성자 임베디드코리아 작성일23-09-12 00:26 조회1,774회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/TestTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android Kotlin"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp"
            />

        <Button
            android:id="@+id/TestBT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            />

        <EditText
            android:id="@+id/TestET"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text"
            android:text="Name"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text"
            android:layout_marginTop="100dp"
            android:background="#C1BDBD"
            android:layout_gravity="center" />

        <Button
            android:id="@+id/bt01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click"
            android:layout_marginTop="10dp"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/tv01"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="#8CBDA5"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp"
            android:layout_gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/etINdata01"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:background="#C3B392"
            android:layout_marginRight="20dp"
            android:layout_marginTop="100dp" />

        <EditText
            android:id="@+id/etINdata02"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:background="#C3B392"
            android:layout_marginTop="100dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/bt02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="실행"
            android:layout_marginTop="10dp"
            />

        <TextView
            android:id="@+id/tv02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="결과"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp" />
    </LinearLayout>

</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var testET: EditText
    private lateinit var editText01: EditText
    private lateinit var INdata01: EditText
    private lateinit var INdata02: EditText

    private lateinit var testButton: Button
    private lateinit var button01: Button
    private lateinit var button02: Button

    private lateinit var testTV: TextView
    private lateinit var textView01: TextView
    private lateinit var textView02: TextView

    var nCount : Int = 0
    var etCount : Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        /* 에디트텍스트 초기화 */
        testET= findViewById(R.id.TestET)
        editText01 = findViewById(R.id.et01)
        //editText01= findViewById<EditText>(R.id.bt01);
        //var editText01: EditText = findViewById(R.id.bt01);
        INdata01= findViewById(R.id.etINdata01)
        INdata02 = findViewById(R.id.etINdata02)
        //editText02= findViewById<EditText>(R.id.bt02);
        //var editText02 : EditText = findViewById(R.id.bt02);

        /*    버튼 초기화  */
        testButton = findViewById(R.id.TestBT)
        button01 = findViewById(R.id.bt01)
        //button01= findViewById<Button>(R.id.bt01);
        //var button01 : Button = findViewById(R.id.bt01);

        button02 = findViewById(R.id.bt02)
        //button02= findViewById<Button>(R.id.bt02);
        //var button02 : Button = findViewById(R.id.bt02);

        /*    텍스트뷰 초기화    */
        testTV = findViewById(R.id.TestTV)
        textView01 = findViewById(R.id.tv01)
        //textView01 = findViewById<TextView>(R.id.textView);
        //var textView01 : TextView = findViewById(R.id.tv01);

        textView02 = findViewById(R.id.tv02)
        //textView02 = findViewById<TextView>(R.id.textView);
        //var textView02 : TextView = findViewById(R.id.tv01);

        /* ---------------------------------------  */

        testTV.setOnClickListener {
            testTV.apply {
                setBackgroundColor(Color.RED)
                text = "Clicked!! ${nCount++}"
                setTextColor(Color.WHITE)
                setTextSize(28.0F)
            }
        }

        testButton.setOnClickListener {
            testButton.apply {
                setBackgroundColor(Color.GREEN)
                setTextColor(Color.BLUE)
                text = "Finish"
                setTextSize(20.0F)
            }
        }

        testET.setOnClickListener {
            testET.apply {
                setBackgroundColor(Color.GRAY)
                setTextColor(Color.BLUE)
                setTextSize(20.0F)
                testTV.text = text.toString()
            }
        }

        button01.setOnClickListener  {

            textView01.text = editText01.text.toString();

        }

        textView02 .setOnClickListener {
            // setOnClickListener는 파라메터로 클릭한 Control인 View 객체를 넘겨준다.
            // 이름을 따로 정의하지 않으면, it으로 되어 있다.
            // it을 TextView로 캐스팅하고 사용할 수 있다.
            val textView02 = it as TextView
            textView02.text = Html.fromHtml(
                "<h1>Hi</h1>HTML<p style=\"color:red;\">Red</idv>")
        }

        INdata01.setOnClickListener  {
            INdata01.setText(null)
        }

        INdata02.setOnClickListener  {
            INdata02.setText(null)
        }

        button02.setOnClickListener {
            button02.apply {
                var aData: Int=0
                var bData: Int=0
                var cData: Int=0

                setBackgroundColor(Color.GREEN)
                setTextColor(Color.BLUE)

                var Astr: String = INdata01.text.toString()
                var Bstr: String = INdata02.text.toString()
                aData = Astr.toInt()
                bData = Bstr.toInt()
                cData = aData + bData

                textView02.text = cData.toString()

                setTextSize(20.0F)
            }
        }

    }
}