ViewBinding 不起作用创建 Android 计算器应用程序
我正在按照这个教程做一个APP --- https://www.youtube.com/watch?v=lTyyNpY7hy0
代码已被弃用,因为它使用 android.extensions, 所以我决定使用 viewBinding 来更新实现,
但是运行后按钮不起作用,因此文本没有显示在输出中,
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.thomas.calculadoracompleta.databinding.ActivityMainBinding
import org.mariuszgromada.math.mxparser.Expression
import java.lang.Exception
import java.text.DecimalFormat
private lateinit var binding: ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.buttonClear.setOnClickListener {
binding.input.text = ""
binding.output.text = ""
}
binding.buttonBracketLeft.setOnClickListener {
binding.input.text = addToInputText("(")
}
binding.buttonBracketRight.setOnClickListener {
binding.input.text = addToInputText(")")
}
binding.buttonDivision.setOnClickListener {
binding.input.text = addToInputText("÷")
}
binding.button7.setOnClickListener {
binding.input.text = addToInputText("7")
}
binding.button8.setOnClickListener {
binding.input.text = addToInputText("8")
}
binding.button9.setOnClickListener {
binding.input.text = addToInputText("9")
}
binding.buttonMultiply.setOnClickListener {
binding.input.text = addToInputText("×")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("4")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("5")
}
binding.button6.setOnClickListener {
binding.input.text = addToInputText("6")
}
binding.buttonSubtraction.setOnClickListener {
binding.input.text = addToInputText("-")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("1")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("2")
}
binding.button3.setOnClickListener {
binding.input.text = addToInputText("3")
}
binding.buttonAddition.setOnClickListener {
binding.input.text = addToInputText("+")
}
binding.button0.setOnClickListener {
binding.input.text = addToInputText("0")
}
binding.buttonDot.setOnClickListener {
binding.input.text = addToInputText(".")
}
binding.buttonEquals.setOnClickListener {
showResult()
}
}
private fun addToInputText(buttonValue: String): String {
return "${binding.input.text}$buttonValue"
}
private fun getInputExpression(): String {
var expression = binding.input.text.replace(Regex("÷"), "/")
expression = expression.replace(Regex("×"),"*")
return expression
}
private fun showResult(){
try {
val expression = getInputExpression()
val result = Expression(expression).calculate()
if(result.isNaN()){
binding.output.text = "Error"
} else {
binding.output.text = DecimalFormat("0.######").format(result).toString()
}
} catch (e: Exception){
}
}
问题是按钮不起作用。
我将添加 xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="@color/window_background"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:padding="30dp"
android:background="@color/io_background"
android:orientation="vertical">
<TextView
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="30sp"
android:textColor="@color/white"
tools:text="5+10-3" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="50sp"
android:textColor="@color/green"
tools:text="12" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="C"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_bracket_left"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="("/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_bracket_right"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text=")"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_division"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="÷"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="7" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="8"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="9"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_multiply"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="×"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="4"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="5"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="6"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_subtraction"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="-"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="1"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="2"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="3"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_addition"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="+"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="0"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_dot"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="."/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_equals"
android:layout_width="0dp"
android:layout_height= "90dp"
android:layout_weight="1"
style="@style/Button_Style"
android:text="="/>
</TableRow>
</TableLayout>
</LinearLayout>
´´´
I was doing an APP following this tutorial ---
https://www.youtube.com/watch?v=lTyyNpY7hy0
Ofc the code was deprecated since it uses android.extensions,
so i decided to go with the updated implementation by using viewBinding
however the after running the buttons are not working and thus text is not being displayed in the output
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.thomas.calculadoracompleta.databinding.ActivityMainBinding
import org.mariuszgromada.math.mxparser.Expression
import java.lang.Exception
import java.text.DecimalFormat
private lateinit var binding: ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.buttonClear.setOnClickListener {
binding.input.text = ""
binding.output.text = ""
}
binding.buttonBracketLeft.setOnClickListener {
binding.input.text = addToInputText("(")
}
binding.buttonBracketRight.setOnClickListener {
binding.input.text = addToInputText(")")
}
binding.buttonDivision.setOnClickListener {
binding.input.text = addToInputText("÷")
}
binding.button7.setOnClickListener {
binding.input.text = addToInputText("7")
}
binding.button8.setOnClickListener {
binding.input.text = addToInputText("8")
}
binding.button9.setOnClickListener {
binding.input.text = addToInputText("9")
}
binding.buttonMultiply.setOnClickListener {
binding.input.text = addToInputText("×")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("4")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("5")
}
binding.button6.setOnClickListener {
binding.input.text = addToInputText("6")
}
binding.buttonSubtraction.setOnClickListener {
binding.input.text = addToInputText("-")
}
binding.button5.setOnClickListener {
binding.input.text = addToInputText("1")
}
binding.button4.setOnClickListener {
binding.input.text = addToInputText("2")
}
binding.button3.setOnClickListener {
binding.input.text = addToInputText("3")
}
binding.buttonAddition.setOnClickListener {
binding.input.text = addToInputText("+")
}
binding.button0.setOnClickListener {
binding.input.text = addToInputText("0")
}
binding.buttonDot.setOnClickListener {
binding.input.text = addToInputText(".")
}
binding.buttonEquals.setOnClickListener {
showResult()
}
}
private fun addToInputText(buttonValue: String): String {
return "${binding.input.text}$buttonValue"
}
private fun getInputExpression(): String {
var expression = binding.input.text.replace(Regex("÷"), "/")
expression = expression.replace(Regex("×"),"*")
return expression
}
private fun showResult(){
try {
val expression = getInputExpression()
val result = Expression(expression).calculate()
if(result.isNaN()){
binding.output.text = "Error"
} else {
binding.output.text = DecimalFormat("0.######").format(result).toString()
}
} catch (e: Exception){
}
}
the problem is that buttons aren't working.
i'll add the xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="@color/window_background"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:padding="30dp"
android:background="@color/io_background"
android:orientation="vertical">
<TextView
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="30sp"
android:textColor="@color/white"
tools:text="5+10-3" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textSize="50sp"
android:textColor="@color/green"
tools:text="12" />
</LinearLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="C"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_bracket_left"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="("/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_bracket_right"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text=")"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_division"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="÷"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="7" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="8"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="9"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_multiply"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="×"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="4"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="5"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="6"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_subtraction"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="-"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="1"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="2"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="3"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_addition"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="+"/>
</TableRow>
<TableRow>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="0"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_dot"
android:layout_width="wrap_content"
android:layout_height= "90dp"
style="@style/Button_Style"
android:text="."/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_equals"
android:layout_width="0dp"
android:layout_height= "90dp"
android:layout_weight="1"
style="@style/Button_Style"
android:text="="/>
</TableRow>
</TableLayout>
</LinearLayout>
´´´
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的绑定放入活动类中
put your binding inside activity class
实际上这是一个 XML 问题,之后修复了它,现在一切都工作正常。
Actually it was a XML problem fixed it after and now everything is working perfectly.