kotlin-BMI計算機を使用してアプリケーションを開発する


Androidアプリケーションページの作成に必要な3つの機能
  • kotlinからなるアクティブファイル
  • アクティブファイルLayoutの
  • を定義します.xmlファイル
  • 表示にアクティビティを追加する期間
  • 1.kotlinで構成されたActivityファイル

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)  //activity_main.xml 파일 layout을 보여줘라
            }
    }
    val heightEditText: EditText = findViewById(R.id.heightEditText)
    val weightEditText = findViewById<EditText>(R.id.weightEditText)
    layoutで定義した各要素を取得して使用する場合は、上のコードのように形状を教える必要があります.

    setOnClickListener,Intent画面切り替え(MainActivity->ResultActivity)

    // resultButton이 클릭 되었을 때 실행 
    resultButton.setOnClickListener {
                val height: Int = heightEditText.text.toString().toInt()
                val weight: Int = weightEditText.text.toString().toInt()
                val intent = Intent(this, ResultActivity::class.java)   // 인텐트 선언            
         // 인텐트로 값 넘겨주기
                intent.putExtra("height", height) 
                intent.putExtra("weight", weight)
                startActivity(intent)
            }

    挿入画面切り替え

    class ResultActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_result) 
            // MainActivity에서 넘겨준 값 받기
            val height = intent.getIntExtra("height", 0)
            val weight = intent.getIntExtra("weight", 0)
            Log.d("ResultActivity", "height : $height weight : $weight")
            }
     }

    2.Layoutを定義します。xmlファイル


    LinearLayoutの使用

    <?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="16dp"
        tools:context=".MainActivity">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/height" // static한 값은 values > string.xml으로 따로 관리해주는 것이 편함
            android:textColor="@color/custom_black" // 색상도 values > color.xml으로 따로 관리해주는 것이 편함
            android:textSize="20sp"
            android:textStyle="bold" />
        <EditText
            android:id="@+id/heightEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:inputType="number" />
            .
            .
            .
        <Button
            android:id="@+id/resultButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="@string/confirm" />
    </LinearLayout>

    3.Activityをインベントリに追加

    <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    </activity>
    <activity android:name=".ResultActivity" /> // 추가