Android Application

Kolin for 문

작성자 임베디드코리아 작성일23-08-27 19:43 조회2,465회 댓글0건

첨부파일

----------------------------------------------------------------------------------------
    MainActivity..kt
----------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //텍스트뷰 초기화
        var textView : TextView = findViewById(R.id.textView);

        //일반적인 for문
        for(i: Int in 1..10)
            textView.append("$i,");    //output : 1, 2, 3, 4, 5 ... 10
        textView.append("\n\n ");
        val len: Int = 5
        for(i in 1..len)
            textView.append("$i,");  //output : 1, 2, 3, 4, 5

        textView.append("\n\n ");
        for(i in 1 until len)
            textView.append("$i,");    //output : 1, 2, 3, 4

        textView.append("\n -----------------------------------\n")
        //증가값 step 키워드를 사용해 증가
        for(i: Int in 1..10 step(2))
            textView.append("$i,  \t\t\t "); //output : 1, 3, 5, 7, 9

        textView.append("\n\n ");

        //아레 downTo 키워드를 사용해 아래로
        for(i in 10 downTo 1)    //output : 10, 9, 8, 7 ... 1
            textView.append("$i,");

        textView.append("\n\n ");
        for(i in 10 downTo 1 step(2))    //output : 10, 8, 6, 4, 2
            textView.append("$i,  \t\t\t ");

        textView.append("\n\n ");
        val n=5
        for (line in 1..n){
            textView.append("  ");
            for(spacd in 1..(n-line))
                textView.append(" ");
            for(star in 1..(2*line-1))
                textView.append("*");

            textView.append("\n")
        }
    }
}

----------------------------------------------------------------------------------------
    activity_main.xml
----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>