android - 在保存状态(变量值)的同时在片段之间切换 [edittexts 和 textviews]
问题描述
我使用 Android Studio (kotlin) 做了一个“导航抽屉活动”。
在 fragment_home.xml 上,我添加了 EditText 和 Buttons。在 MainActivity.kt 上,我添加了计算,以便当按下 Button 时,结果会显示在同一 fragment_home 上的 TextView 中。
问题是当我滑动导航抽屉,并从 fragment_home 更改为另一个片段,然后回到相同的 fragment_home 时,按钮和编辑文本根本不起作用。
请帮忙,我是新手,正在寻找解决方案很多天。
基本上输入了一些值,并且有两个按钮可用:计算和重置。逻辑是,如果任何 inputtext 为空,它将显示消息“输入正确的值”,如果点击重置按钮,它会将值重置为预设值,默认值是浮点数... mainactivity.kt 的代码如下:
类 MainActivity : AppCompatActivity() { 私有 lateinit var appBarConfiguration:AppBarConfiguration 私有 lateinit var 绑定:ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
//values from editText are imported to variables
val inputcatload2 = findViewById(R.id.inputcatload1) as EditText
val inputcatconv2 = findViewById(R.id.inputcatconv1) as EditText
val inputcfr2 = findViewById(R.id.inputcfr1) as EditText
//values from Textviews are imported to variables
val outputcatflow2 = findViewById(R.id.outputcatflow1) as TextView
val outputdistillateflow2 = findViewById(R.id.outputdistillateflow1) as TextView
val outputtotalfeed2 = findViewById(R.id.outputtotalfeed1) as TextView
val outputh1passessv2 = findViewById(R.id.outputh1passessv1) as TextView
//display of error message and declaration of two buttons
val msg1 = findViewById(R.id.message1) as TextView
val btn1 = findViewById(R.id.button1) as Button
val btn2 = findViewById(R.id.button2) as Button
//if any input is empty, message "enter proper input" is displayed else calculation is done
btn1.setOnClickListener {
inputcatload2.text.toString()
if (inputcatload2.length() == 0 || inputcatload2.text.toString() == ".") {
val msg2="Please input correct Load"
msg1.setText(msg2)
} else if (inputcatconv2.length() == 0 || inputcatconv2.text.toString() == "."){
val msg2="Please input correct Cat. Conversion"
msg1.setText(msg2)
} else if (inputcfr2.length() == 0 || inputcfr2.text.toString() == "."){
val msg2="Please input correct CFR"
msg1.setText(msg2)
} else {
val msg2="*** RESULTS ***"
msg1.setText(msg2)
val val1 = inputcatload2.text.toString().toFloat()
val val2 = inputcatconv2.text.toString().toFloat()
val val3 = inputcfr2.text.toString().toFloat()
val result1 = val1 * 148.72 / 100;
val rounded1 = String.format("%.2f", result1)
outputcatflow2.setText(rounded1)
val result2 = val1 * 148.72/100 * val3;
val rounded2 = String.format("%.1f", result2)
outputtotalfeed2.setText(rounded2)
val result3 = (val1 * 148.72/100 * val3) - (val1 * 148.72/100);
val rounded3 = String.format("%.2f", result3)
outputdistillateflow2.setText(rounded3)
val result4 = (val1 * 148.72/100 * val3)/2;
val rounded4 = String.format("%.1f", result4)
outputh1passessv2.setText(rounded4)
}
}
//this is reset button which sets all inputs to default float values
btn2.setOnClickListener{
inputcatload2.setText("100")
inputcatconv2.setText("31.2")
inputcfr2.setText("1.24")
msg1.setText("")
outputcatflow2.setText("")
outputdistillateflow2.setText("")
outputtotalfeed2.setText("")
outputh1passessv2.setText("")
}
//everything is unchanged here
setSupportActionBar(binding.appBarMain.toolbar)
binding.appBarMain.fab.setOnClickListener { view ->
Snackbar.make(view, "Built by Umair Fareed\nFor personal use only. Not to be distributed.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
解决方案
推荐阅读
- sorting - StreamAPI 上的 Flink 高效排序
- java - java.sql.SQLException:找不到适合 jdbc:mysql://localhost:3306/mydatabase Android Studio Java MySQL 的驱动程序
- angular - 无法从 Angular 应用程序调用外部 API,出现 CORS 问题
- swift - 如何访问 SwiftUI 中的子视图?
- android - 更新旋转后的 ViewRenderable 点击处理
- python - Python 使用 lamda 函数错误缺少 1 个必需的位置参数:“事件”
- c# - 在 C#/.NET 中以编程方式查找正在使用某个 USB 设备(通过设备 ID)的进程
- javascript - 创建“自动复制到剪贴板”javascript
- api - ActiveCollab 集成选项(将客户添加到项目、SMS 到笔记等)
- artifact - 从 CLI 阅读 Jfrog Artifact 的 X 射线报告