Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin val和var不在onClickListener外部工作_Kotlin - Fatal编程技术网

Kotlin val和var不在onClickListener外部工作

Kotlin val和var不在onClickListener外部工作,kotlin,Kotlin,这里的新手 当我将var和val放在updateEditTextSpecificGravity.addTextChangedListener和按钮CalculateForMula.setOnClickListener中时,代码会工作,但只要我将其移到这些监听器之外,就不会发生任何事情。我应该将它们标记为全局变量吗 活动\u main.xml: <?xml version="1.0" encoding="utf-8"?> <TableLay

这里的新手

当我将
var
val
放在
updateEditTextSpecificGravity.addTextChangedListener
按钮CalculateForMula.setOnClickListener
中时,代码会工作,但只要我将其移到这些监听器之外,就不会发生任何事情。我应该将它们标记为全局变量吗

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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">
<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Tank 1 (Kg)"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <EditText
        android:id="@+id/editTextTank1"
        android:layout_column="2"
        android:layout_gravity="center"
        android:text="@string/editTextTank1Hint_0"
        android:digits="0123456789."
        android:layout_width="90dp"
        android:layout_height="38dp"
        android:ems="10"
        android:inputType="numberDecimal|number"
        android:importantForAutofill="no" />

    <TextView
        android:hint="6000kg"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Center (Kg)"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <EditText
        android:id="@+id/editTextTankCenter"
        android:layout_column="2"
        android:text="@string/editTextTankCenterHint_0"
        android:digits="0123456789."
        android:layout_width="90dp"
        android:layout_height="38dp"
        android:ems="10"
        android:inputType="numberDecimal|number" />

    <TextView
        android:hint="10000kg"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Tank 2 (Kg)"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/editTextTank2"
        android:layout_column="2"
        android:text="@string/editTextTank2Hint_0"
        android:digits="0123456789."
        android:label="Tank 2"
        android:layout_width="90dp"
        android:layout_height="38dp"
        android:ems="10"
        android:inputType="numberDecimal|number" />

    <TextView
        android:hint="6000kg"
        android:layout_column="3"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Fuel On Board"
        android:layout_column="1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textViewFOB"
        android:text="0"
        android:layout_column="2"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

    <TextView
        android:layout_column="2"
        android:text="22000Kg"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Fuel Required (Kg)"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/editTextFuelRequiredInKg"
        android:layout_column="2"
        android:text="@string/editTextFuelRequiredInKgHint_0"
        android:digits="0123456789."
        android:inputType="numberDecimal|number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />

</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Uplift in Kg"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textViewFuelUpliftInKg"
        android:layout_column="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Specific Gravity"
        android:layout_column="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <EditText
        android:id="@+id/editTextSpecificGravity"
        android:layout_column="2"
        android:text="@string/editTextSpecificGravityHint_0"
        android:digits="0123456789."
        android:inputType="numberDecimal|number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Uplift In Liters"
        android:layout_column="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/textViewFuelUpliftInLt"
        android:layout_column="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="Percentage"
        android:layout_column="1"
        android:layout_gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textViewPercentage"
        android:layout_column="2"
        android:hint="@string/textViewPercentage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <SeekBar
        android:id="@+id/seekBarSpecificGravity"
        android:layout_span="3"
        android:layout_width="200dp"
        android:layout_height="30dp" />
</TableRow>


<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/buttonCalculate"
        android:layout_column="3"
        android:layout_gravity="center"
        android:text="@string/buttonCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="9dp" />

</TableRow>
package com.example.fuelcalculation3

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Button
import android.widget.EditText
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val buttonCalculateFormula = findViewById<Button>(R.id.buttonCalculate)
    val updateTextViewFOB = findViewById<TextView>(R.id.textViewFOB)
    val updateTextViewFuelRequiredInKg = findViewById<TextView>(R.id.textViewFuelUpliftInKg)
    val updateTextViewFuelUpliftInLt = findViewById<TextView>(R.id.textViewFuelUpliftInLt)
    val updateEditTextSpecificGravity = findViewById<EditText>(R.id.editTextSpecificGravity)
    val updateTextViewPercentage = findViewById<TextView>(R.id.textViewPercentage)
    val getSeekBarSpecificGravity = findViewById<SeekBar>(R.id.seekBarSpecificGravity)
    var intEditTextTank1 = findViewById<EditText>(R.id.editTextTank1).text.toString().toInt()
    var intEditTextTankCenter = findViewById<EditText>(R.id.editTextTankCenter).text.toString().toInt()
    var intEditTextTank2 = findViewById<EditText>(R.id.editTextTank2).text.toString().toInt()
    var fuelOnBoard = intEditTextTank1 + intEditTextTankCenter + intEditTextTank2
    var intEditTextFuelRequiredInKg = findViewById<EditText>(R.id.editTextFuelRequiredInKg).text.toString().toInt()
    var intEditTextSpecificGravity = findViewById<EditText>(R.id.editTextSpecificGravity).text.toString().toDouble()
    var upliftRequiredInKg = intEditTextFuelRequiredInKg - fuelOnBoard
    var upliftRequiredInLt = (upliftRequiredInKg / intEditTextSpecificGravity)
    var calculatedPercentage = ((intEditTextFuelRequiredInKg - fuelOnBoard ) * 100 ) / ( upliftRequiredInLt * intEditTextSpecificGravity ).toFloat()

    getSeekBarSpecificGravity.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean){
            val updatedSeekBarValue = (i * 0.01)
            //To change the value in and EditText we use "setText"
            updateEditTextSpecificGravity.setText("$updatedSeekBarValue")
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {
//                Toast.makeText(applicationContext, "stop tracking", Toast.LENGTH_SHORT).show()
        }

        override fun onStopTrackingTouch(seekBar: SeekBar?) {
//                Toast.makeText(applicationContext, "start tracking", Toast.LENGTH_SHORT).show()
        }
    })

    updateEditTextSpecificGravity.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) {
            updateTextViewPercentage.setText("$calculatedPercentage")
        }
    })

    buttonCalculateFormula.setOnClickListener {
        updateTextViewFOB.text = fuelOnBoard.toString()+" Kg(FOB)"
        updateTextViewFuelRequiredInKg.text = upliftRequiredInKg.toString()+" Kg(Req)"
        updateTextViewFuelUpliftInLt.text = upliftRequiredInLt.toString()+" Lt (required)"
        updateTextViewPercentage.text = calculatedPercentage.toString()+"%"
    }
}
}