iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
1
Software Development

高中生Kotlin實作30天系列 第 7

Day07 電阻電流計算機(上)(Spinner)

  • 分享至 

  • xImage
  •  

Latout

Spinner

下拉式的選單,一般只占用一個Item的大小,當被點選時會展開清單

<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content" android:id="@+id/spinner1"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginStart="8dp" app:layout_constraintEnd_toStartOf="@+id/spinner2"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content" android:id="@+id/spinner2"
    app:layout_constraintStart_toEndOf="@+id/spinner1"
    android:layout_marginStart="8dp"
    app:layout_constraintTop_toTopOf="@+id/spinner1" app:layout_constraintEnd_toStartOf="@+id/spinner3"
    android:layout_marginEnd="8dp"/>
<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content" android:id="@+id/spinner3"
    android:layout_marginStart="8dp"
    app:layout_constraintEnd_toStartOf="@+id/spinner4" android:layout_marginEnd="8dp"
    app:layout_constraintStart_toEndOf="@+id/spinner2"
    app:layout_constraintTop_toTopOf="@+id/spinner2"/>
<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content" android:id="@+id/spinner4"
    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
    app:layout_constraintStart_toEndOf="@+id/spinner3" android:layout_marginStart="8dp"
    android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>

Method

Adapter

在Android中,資料來源和顯示畫面的處理是分開的,只有在需要的時候,才會將資料轉換成畫面顯示,負責轉換的介面就是Adapter
Android SDK本身有提供現成的資源可以直接使用。當需要較複雜的畫面,同時包含圖片、文字等,也可以實作自定義的Adapter

因為只需要顯示文字,所以使用Android SDK中的資源

//建立資料
val colorlist1 = arrayOf("黑","棕","紅","橘","黃","綠","藍","紫","灰","白")
val colorlist2 = arrayOf("棕","紅","綠","藍","紫","灰","金","銀")
val colorlist3 = arrayOf("黑","棕","紅","橘","黃","綠","藍","紫","灰","白","金","銀")

//設置Adapter,並放入資料
val adapter1 = android.widget.ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, colorlist1)
val adapter2 = android.widget.ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, colorlist2)
val adapter3 = android.widget.ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, colorlist3)

//連接Adapter
spinner1.setAdapter(adapter1)
spinner2.setAdapter(adapter1)
spinner3.setAdapter(adapter2)
spinner4.setAdapter(adapter3)

getSelectedItemPosition()

使用getSelectedItemPosition()可以得到現在被選則的項目是第幾個,當第一項被選擇時為0,第二項被選擇時為1,以此類推

var tens = spinner1.getSelectedItemPosition()*10
var units = spinner2.getSelectedItemPosition()
var mul = spinner3.getSelectedItemPosition()
var tol = spinner4.getSelectedItemPosition()

計算

pow()

用於計算指數
輸入兩個浮點數(Double)a,b,回傳a^b

when(mul){
    10-> mul = -1
    11 -> mul = -2
}
var resistance = (tens+units)*Math.pow(10.0,mul.toDouble())

為了方便閱讀,使用十進位單位符號做簡化

if (resistance < 1000) {
    tv_res.text = "電阻:$resistance Ω"
} else if (resistance < 1000000) {
    tv_res.text = "電阻:${resistance / 1000} kΩ"
} else if (resistance < 1000000000) {
    tv_res.text = "電阻:${resistance / 1000000} MΩ"
} else {
    tv_res.text = "電阻:${resistance / 1000000000} GΩ"
}

顯示容差

when(tol){
    0 -> {
        tv_tol.text = "容差:1%"
    }
    1 -> {
        tv_tol.text = "容差:2%"
    }
    2 -> {
        tv_tol.text = "容差:0.5%"
    }
    3 -> {
        tv_tol.text = "容差:0.25%"
    }
    4 -> {
        tv_tol.text = "容差:0.1%"
    }
    5 -> {
        tv_tol.text = "容差:0.05%"
    }
    6 -> {
        tv_tol.text = "容差:5%"
    }
    7 -> {
        tv_tol.text = "容差:10%"
    }
}

實作成果


上一篇
Day 06 RGBA調色盤(SeekBar)
下一篇
Day 08電阻電流計算機(下) (startActivity)
系列文
高中生Kotlin實作30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言