gpt4 book ai didi

kotlin - 如何在 android Kotlin 中禁用点按钮

转载 作者:行者123 更新时间:2023-12-02 12:15:41 24 4
gpt4 key购买 nike

我是 Android Studio - Kotlin 的新手,我正在构建一个带有数字和带小数点的数学运算符的应用程序(.dot 按钮)在我的数学计算器应用程序中,如果按下点按钮,该按钮应该被禁用或未输入第二次。如果多次按下,我想禁用或停止打印点按钮 (.)。

这是我的代码

Acitivity.kt文件

package com.tripbegins.calculator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.tripbegins.calculator.R.id.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

var emptyText = true
fun numberEvents(view: View) {
var checkButton:Boolean = false
if(emptyText){
viewResult.setText("")
}
emptyText = false

var button = view as Button
var isClicked = viewResult.text.toString()
when (button.id) {
buttonOne.id -> isClicked += "1"
buttonTwo.id -> isClicked += "2"
buttonThree.id -> isClicked += "3"
dotButton.id-> isClicked+="."
}

viewResult.setText(isClicked)
}

var operation ="+"
var oldValues:String? = null
fun mathOperation(view: View){
var mathButton = view as Button
var isClicked = viewResult.text.toString()

when(mathButton.id){
plusButton.id-> {
operation = "+"
}
minusButton.id->{
operation="-"
}
mulButton.id->{
operation="*"
}

}
oldValues = viewResult.text.toString()
emptyText = true
}

fun Calculate(view: View){

var newValues = viewResult.text.toString()
var calulateButton:Double? = null
when(operation){
"+"-> { calulateButton = oldValues!!.toDouble() + newValues.toDouble() }
"-"-> { calulateButton = oldValues!!.toDouble() - newValues.toDouble() }
"*"-> { calulateButton = oldValues!!.toDouble() * newValues.toDouble() }

}
viewResult.setText(calulateButton.toString())
emptyText=true
}
fun clearFunction(view: View){
viewResult.setText("")
}
}

activity.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.tripbegins.calculator.MainActivity">

<EditText
android:id="@+id/viewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:ems="10"
android:gravity="bottom|right"
android:inputType="textPersonName"
android:text="0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="76dp"
android:onClick="numberEvents"
android:text="2"
app:layout_constraintStart_toEndOf="@+id/buttonOne"
app:layout_constraintTop_toBottomOf="@+id/viewResult" />

<Button
android:id="@+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="38dp"
android:onClick="mathOperation"
android:text="+"
app:layout_constraintBaseline_toBaselineOf="@+id/minusButton"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="33dp"
android:onClick="mathOperation"
android:text="-"
app:layout_constraintStart_toEndOf="@+id/plusButton"
app:layout_constraintTop_toBottomOf="@+id/buttonTwo" />

<Button
android:id="@+id/mulButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:onClick="mathOperation"
android:text="*"
app:layout_constraintBaseline_toBaselineOf="@+id/minusButton"
app:layout_constraintStart_toEndOf="@+id/minusButton" />

<Button
android:id="@+id/calButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:onClick="Calculate"
android:text="Calculate"
app:layout_constraintStart_toStartOf="@+id/minusButton"
app:layout_constraintTop_toBottomOf="@+id/minusButton" />

<Button
android:id="@+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:onClick="numberEvents"
android:text="3"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonTwo"
app:layout_constraintStart_toEndOf="@+id/buttonTwo" />

<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:onClick="numberEvents"
android:text="1"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonTwo"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/dotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="22dp"
android:onClick="numberEvents"
android:text="."
app:layout_constraintBaseline_toBaselineOf="@+id/calButton"
app:layout_constraintEnd_toStartOf="@+id/calButton" />

<Button
android:id="@+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:onClick="clearFunction"
android:text="Clear"
app:layout_constraintBaseline_toBaselineOf="@+id/calButton"
app:layout_constraintStart_toEndOf="@+id/calButton" />
</android.support.constraint.ConstraintLayout>

我对结果的期望

有效输入 -> 3.(.dot 按钮不应输入第二次)

无效输入 -> 3...(目前我可以输入多个 .dot 按钮)

点按钮不应按两次。它应该只激活一次。

谢谢穆罕默德

最佳答案

所以你想在用户第一次点击名称为 dotButton 的按钮后禁用它:

要禁用按钮,请使用:

dotButton.isClickable=false

如果你想让按钮再次变得可点击(比如当用户删除他已经按下的点时)你可以使用:

dotButton.isClickable=true

编辑:像那样修改您的事件代码:

when (button.id) {
buttonOne.id -> isClicked += "1"
buttonTwo.id -> isClicked += "2"
buttonThree.id -> isClicked += "3"
dotButton.id-> {
isClicked+="."
dotButton.isClickable=false//**add this line here**

}
}

fun clearFunction(view: View){
viewResult.setText("")
dotButton.isClickable=true//**add this line here**
}

关于kotlin - 如何在 android Kotlin 中禁用点按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47272245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com