我怎樣才能把計算后的數字放在文本視圖上?如果需要,我可以共享更多檔案
fun calcularIdade() {
val editTextHello = findViewById<TextInputEditText>(R.id.Date)
var num= Integer.parseInt(editTextHello.toString())
num = 2022-num
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
findViewById<TextView>(R.id.textView1).setText(num.toString())
}
}
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/Date"
android:hint="“Escreva o seu ano de nascimento!”"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textDT"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:id="@ id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:text="TextView" />
<Button
android:id="@ id/B1"
android:onClick="calcularIdade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:text="Calcular" />
</LinearLayout>
</RelativeLayout>
activity_data_nascimento.xml 主活動
安卓作業室
計算后需要在文本視圖上設定一個數字我已經嘗試了所有但我不能我的主要問題是將值從字串更改為整數計算然后將值寫入文本視圖
uj5u.com熱心網友回復:
利用var num= Integer.parseInt(editTextHello.text.toString())
反而var num= Integer.parseInt(editTextHello.toString())
您必須決議TextInputLayout中的值
uj5u.com熱心網友回復:
較短的版本
val num = editTextHello.text.toString().toInt()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/533911.html
上一篇:C 中的一行用戶輸入賦值
