我需要添加功能为+/-的新按钮。如果用户第一次点击该按钮,则该值应返回 - > “ - ”(ex => -123),如果用户再次点击,则应删除“ - ”(ex => 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("")
}
}
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>
更新了Activity.kt文件 我已经更新了= / - 的代码。在函数外添加了var
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
更新了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.*
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**
}
}
更新了按钮的xml文件 - &gt; +/-
<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" />
答案 0 :(得分:1)
修改强>:
<!DOCTYPE html>
<html>
<body>
<input type="text" name="example" list="exampleList">
<datalist id="exampleList">
<option value="017">
<option value="018">
<option value="019">
</datalist>
</body>
</html>