带有 EditText 的加号和减号按钮
Plus and minus button with EditText
我有一个可以正常使用递增和递减功能的按钮。但我需要我也可以使用 EdidText 手动输入值。
我的代码如下:
Activity.tk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal var minteger = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val plus = findViewById<Button>(R.id.increase)
val minus = findViewById<Button>(R.id.decrease)
plus.setOnClickListener {
increaseInteger(plus)
}
minus.setOnClickListener {
decreaseInteger(minus)
}
}
fun increaseInteger(view: View) {
minteger += 1
display(minteger)
}
fun decreaseInteger(view: View) {
minteger -= 1
display(minteger)
}
private fun display(number: Int) {
val displayInteger = findViewById<View>(
R.id.integer_number) as TextView
displayInteger.text = "" + number
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:orientation="horizontal">
<Button
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="16dp"
android:text="0"
android:textStyle="bold"
android:textSize="70sp" />
<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
我已经用 EditText 更改了 TextView (@+id/integer_number
)
但是当我输入一个没有保存的值时,我找不到办法让在EdidText中输入的值被保存,以便我可以增加或减少它。
如果有任何能够操作我的按钮的建议,我将不胜感激。
在你的 xml 中更改为 EditText
:
<EditText
android:id="@+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="16dp"
android:text="0"
android:inputType="number"
android:textStyle="bold"
android:textSize="70sp" />
我添加了属性:
android:inputType="number"
所以只允许使用数字。
在您的 MainActivity
class 中添加此导入:
import kotlinx.android.synthetic.main.activity_main.*
因此您可以访问 xml 的所有项目而无需 findViewById()
。
现在您不需要在任何地方保存 EditText
的值,因为您可以随时通过以下方式获取它:
integer_number.text.toString().toInt()
因此不再需要变量 minteger
。
将 activity 的代码更改为:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase.setOnClickListener { increaseInteger() }
decrease.setOnClickListener { decreaseInteger() }
}
fun increaseInteger() {
display(integer_number.text.toString().toInt() + 1)
}
fun decreaseInteger() {
display(integer_number.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number.setText("$number")
}
}
我也做了其他的简化。
您不需要函数 increaseInteger()
和 decreaseInteger()
.
的任何参数
最大和最小整数值有限制。
因此,如果您想安全起见,还必须在 increaseInteger()
和 decreaseInteger()
中使用 try/catch
块以避免异常。
我有一个可以正常使用递增和递减功能的按钮。但我需要我也可以使用 EdidText 手动输入值。
我的代码如下:
Activity.tk
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal var minteger = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val plus = findViewById<Button>(R.id.increase)
val minus = findViewById<Button>(R.id.decrease)
plus.setOnClickListener {
increaseInteger(plus)
}
minus.setOnClickListener {
decreaseInteger(minus)
}
}
fun increaseInteger(view: View) {
minteger += 1
display(minteger)
}
fun decreaseInteger(view: View) {
minteger -= 1
display(minteger)
}
private fun display(number: Int) {
val displayInteger = findViewById<View>(
R.id.integer_number) as TextView
displayInteger.text = "" + number
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:orientation="horizontal">
<Button
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="16dp"
android:text="0"
android:textStyle="bold"
android:textSize="70sp" />
<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
我已经用 EditText 更改了 TextView (@+id/integer_number
)
但是当我输入一个没有保存的值时,我找不到办法让在EdidText中输入的值被保存,以便我可以增加或减少它。
如果有任何能够操作我的按钮的建议,我将不胜感激。
在你的 xml 中更改为 EditText
:
<EditText
android:id="@+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="16dp"
android:text="0"
android:inputType="number"
android:textStyle="bold"
android:textSize="70sp" />
我添加了属性:
android:inputType="number"
所以只允许使用数字。
在您的 MainActivity
class 中添加此导入:
import kotlinx.android.synthetic.main.activity_main.*
因此您可以访问 xml 的所有项目而无需 findViewById()
。
现在您不需要在任何地方保存 EditText
的值,因为您可以随时通过以下方式获取它:
integer_number.text.toString().toInt()
因此不再需要变量 minteger
。
将 activity 的代码更改为:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase.setOnClickListener { increaseInteger() }
decrease.setOnClickListener { decreaseInteger() }
}
fun increaseInteger() {
display(integer_number.text.toString().toInt() + 1)
}
fun decreaseInteger() {
display(integer_number.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number.setText("$number")
}
}
我也做了其他的简化。
您不需要函数 increaseInteger()
和 decreaseInteger()
.
的任何参数
最大和最小整数值有限制。
因此,如果您想安全起见,还必须在 increaseInteger()
和 decreaseInteger()
中使用 try/catch
块以避免异常。