Android Studio:如何将EditText输入值计算后设置到TextView?
如何将计算后的数值显示到TextView上?
我使用Android Studio开发,需求是将计算后的数值设置到TextView中,尝试了各种方法但都失败了,主要问题在于字符串转整数、计算后将值写入TextView的环节。以下是我的代码实现:
原Kotlin代码
fun calcularIdade() { val editTextHello = findViewById<TextInputEditText>(R.id.Date) var num= Integer.parseInt(editTextHello.toString()) num = 2022-num Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show() findViewById<TextView>(R.id.textView1).setText(num.toString()) } }
原XML布局代码
<com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Date" android:hint="“Escreva o seu ano de nascimento!”" android:inputType="number"/> </com.google.android.material.textfield.TextInputLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textDT" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" android:orientation="horizontal"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:layout_weight="1" android:text="TextView" /> <Button android:id="@+id/B1" android:onClick="calcularIdade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_weight="1" android:text="Calcular" /> </LinearLayout> </RelativeLayout>
问题分析与修正方案
核心错误点
- 获取输入文本错误:
editTextHello.toString()是将EditText控件对象转为字符串(结果类似com.google.android.material.textfield.TextInputEditText{...}),而非获取用户输入的内容,正确写法是editTextHello.text.toString()。 - 未处理异常输入:直接用
Integer.parseInt会在输入为空或非数字时抛出崩溃,需添加异常处理。 - 固定年份不合理:硬编码2022会导致年份过时,建议用当前系统年份计算。
- 重复调用findViewById:多次调用
findViewById影响性能,建议提前缓存控件引用。
修正后的Kotlin代码
import java.util.Calendar fun calcularIdade() { // 缓存控件引用 val birthYearInput = findViewById<TextInputEditText>(R.id.Date) val resultTextView = findViewById<TextView>(R.id.textView1) // 获取输入文本并处理空值 val inputText = birthYearInput.text?.toString()?.trim() ?: "" if (inputText.isEmpty()) { Toast.makeText(this, "请输入出生年份", Toast.LENGTH_SHORT).show() return } try { // 字符串转整数 val birthYear = inputText.toInt() // 获取当前年份 val currentYear = Calendar.getInstance().get(Calendar.YEAR) val age = currentYear - birthYear // 显示计算结果到TextView resultTextView.text = age.toString() Toast.makeText(this, "计算完成", Toast.LENGTH_SHORT).show() } catch (e: NumberFormatException) { // 处理非数字输入 Toast.makeText(this, "请输入有效的数字年份", Toast.LENGTH_SHORT).show() } }
额外优化建议
- 使用View Binding替代
findViewById,更安全且代码更简洁:- 在模块的
build.gradle中启用View Binding:android { ... buildFeatures { viewBinding true } } - 修改代码使用View Binding:
private lateinit var binding: ActivityDataNascimentoBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityDataNascimentoBinding.inflate(layoutInflater) setContentView(binding.root) } fun calcularIdade() { val inputText = binding.date.text?.toString()?.trim() ?: "" // 后续逻辑同上,直接用binding.textView1设置文本 }
- 在模块的
内容的提问来源于stack exchange,提问作者Leo
相关产品推荐
相关产品推荐

