作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要添加具有+/-功能的新按钮。如果用户第一次单击该按钮,则值应返回->“-”(例如=> -123),如果用户再次单击该值,则应删除值“-”(例如=> 123)。该功能类似于计算器中的+/-按钮。
这是我的代码
Activity.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("")
}
}
<?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>
var emptyText = true
var isMinusPrinted=false
fun numberEvents(view: View)
**Current result**
If button clicked for the first time value => -123
If button clicked for the second time value => -123
If button clicked for the third time value => --123
**Expected Result**
If button clicked for the first time value => -123
If button clicked for the second time value => 123
If button clicked for the third time value => -123
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.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
var emptyText = true
var isMinusPrinted=false
fun numberEvents(view: View) {
if(emptyText){
viewResult.setText("")
}
emptyText = false
var button = view as Button
var plusMinusCheck =""
var isClicked = viewResult.text.toString()
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**
}
plusMinusBtn.id->{
var resultWithoutMinus=isClicked//ex: 123
if(!isMinusPrinted){
isClicked = "-" + resultWithoutMinus//this line for
example give result like: -123
isMinusPrinted=true
}
else{
isClicked=resultWithoutMinus //result became : 123
isMinusPrinted=false
}
}
}
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
dotButton.isClickable=true//**add this line here**
}
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
dotButton.isClickable=true //**add this line here**
}
fun clearFunction(view: View){
viewResult.setText("")
dotButton.isClickable=true //**add this line here**
}
}
<Button
android:id="@+id/plusMinusBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="52dp"
android:layout_marginStart="38dp"
android:onClick="numberEvents"
android:text="+/-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
最佳答案
编辑:
var emptyText = true
var isMinusPrinted=false//add this line here
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+="."
plusMinusBtn.id->{//add this block of code from here
var resultWithoutMinus=Math.abs(isClicked.toInt()) //ex: 123
if(!isMinusPrinted){
isClicked = "-" + resultWithoutMinus//this line for example give result like: -123
isMinusPrinted=true
}
else{
isClicked=resultWithoutMinus //result became : 123
isMinusPrinted=false
}
}//to here
}
viewResult.setText(isClicked)
}
关于android-studio - 如何在Kotlin Android中为屁股添加+/-功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47295703/
我是一名优秀的程序员,十分优秀!