查了kotlin的寫法,更確定在課堂上學的這個寫法了
Java的寫法是 settext
而kotlin是TextView.text =
沒想到光是改成kotlin的寫法就不會閃退了,困惑
blogger不能夠方便的放上程式碼,想跑路啦
原來有個插件可以用,但還是些微的麻煩
<?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/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="打折比例(%)"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.149"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editnumber"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:ems="10"
android:inputType="number"
android:text="$0.00"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.447"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="28dp"
android:text="打折後價格"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="15dp"
android:text="0%"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/editnumber"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="29dp"
android:text="0"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/percent" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="253dp"
android:layout_height="47dp"
android:layout_marginTop="144dp"
android:layout_marginEnd="96dp"
android:layout_marginRight="96dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editnumber" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.seekbarpercentage
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.SeekBar
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private lateinit var money : TextView
private lateinit var percentage : TextView
private lateinit var seekBar: SeekBar
private lateinit var editnumber: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
money = findViewById<TextView>(R.id.money)
percentage = findViewById<TextView>(R.id.percent)
seekBar = findViewById<SeekBar>(R.id.seekBar)
editnumber = findViewById<EditText>(R.id.editnumber)
var pg = 0
editnumber.setText("0")
// progressBar
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
percentage.text = "$progress%"
money.text = "${editnumber.text.toString().toFloat() * progress/100}"
pg= progress
//pg 為外部自定義變數,用來儲存 progress 供其他地方使用
}
override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {}
})
editnumber.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if(editnumber.text.toString() != "")
money.setText("${editnumber.text.toString().toFloat() * (pg/100)}")
else money.text = "0"
}
})
}
}
沒有留言:
張貼留言