如何在安卓 Kotlin 中使用 CheckBox?


本示例演示如何在 Android Kotlin 中使用 CheckBox。

步驟 1-在 Android Studio 中新建一個專案,轉到 File⇒New Project,填寫所有必需的詳細資訊來建立新專案。

步驟 2-將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp"
   tools:context="MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp"
      android:text="Tutorials Point"
      android:textAlignment="center"
      android:textColor="@android:color/holo_green_dark"
      android:textSize="32sp"
      android:textStyle="bold" />
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/checkBox1"
      android:layout_marginBottom="30dp"
      android:text="Select your products!"
      android:textAlignment="center"
      android:textColor="@android:color/background_dark"
      android:textSize="24sp"
      android:textStyle="bold|italic" />
   <CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Pizza" />
   <CheckBox
      android:id="@+id/checkBox2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/checkBox1"
      android:layout_centerInParent="true"
      android:layout_marginTop="10dp"
      android:text="Coffee" />
   <CheckBox
      android:id="@+id/checkBox3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/checkBox2"
      android:layout_centerInParent="true"
      android:layout_marginTop="10dp"
      android:text="Burger" />
   <Button
      android:id="@+id/button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/checkBox3"
      android:layout_marginTop="10dp"
      android:text="Complete purchase" />
</RelativeLayout>

步驟 3-將以下程式碼新增到 src/MainActivity.kt

import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.lang.StringBuilder
class MainActivity : AppCompatActivity() {
   lateinit var pizza: CheckBox
   lateinit var coffee: CheckBox
   lateinit var burger: CheckBox
   lateinit var button: Button
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      pizza = findViewById(R.id.checkBox1)
      coffee = findViewById(R.id.checkBox2)
      burger = findViewById(R.id.checkBox3)
      button = findViewById(R.id.button)
      button.setOnClickListener {
         var totalAmount: Int = 0
         val result = StringBuilder()
         result.append("Selected Items")
         if (pizza.isChecked) {
            result.append("
Pizza 100Rs")             totalAmount += 100          }          if (coffee.isChecked) {             result.append("
Coffee 50Rs")             totalAmount += 50          }          if (burger.isChecked) {             result.append("
Burger 120Rs")             totalAmount += 120          }          result.append("
Total: " + totalAmount + "Rs")          Toast.makeText(applicationContext, result.toString(), Toast.LENGTH_SHORT).show()       }    } }

步驟 4-將以下程式碼新增到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.kotlipapp">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們試著執行你的應用程式。我假設你已經將你的 Android 移動裝置與你的電腦連線。要從 Android Studio 執行應用程式,開啟一個你的專案的活動檔案,並點選工具欄中的執行 圖示。選擇你的移動裝置作為一個選項,然後檢查你的移動裝置,它會顯示你的預設螢幕−

點選 此處 下載專案程式碼。

更新於: 2020 年 4 月 21 日

3K+ 瀏覽

開啟您的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.