这看起来应该很容易修复,有几个帖子带有不同示例的示例代码,更复杂。然而,没有人为这个简单的计划工作过。
BMI计算器有两个活动,MainActivity和BMIResultsScreen。用户在MainActivity中输入他们的体重和身高,并在BMIResultsScreen中查看结果。然后,他们可以选择单击按钮退出,或单击按钮返回MainActivity。退出按钮有效,但返回MainActivity的按钮不起作用。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.username.bmicalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BMIResultsScreen">
<intent-filter>
<action android:name="com.example.username.BMIResultsScreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
MainActivity:
package com.example.username.bmicalculator
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setOnClickListenerForButton()
}
fun calculateBMI(): Double {
var weight: Double = (numWeight.getText().toString().toDouble())
var height: Double = (numHeight.getText().toString().toDouble())
var calculatedBMI = (((weight / height) / height))
return calculatedBMI
}
fun setOnClickListenerForButton() {
btnCalculateBMI.setOnClickListener {
val intent = Intent("com.example.username.BMIResultsScreen")
intent.putExtra("BMIResult", calculateBMI())
startActivity(intent)
}
}
}
第二项活动,BMIResultScreen:
package com.example.username.bmicalculator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import kotlinx.android.synthetic.main.activity_bmiresults_screen.*
import java.text.DecimalFormat
class BMIResultsScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bmiresults_screen)
showBMIResult()
findBMICategory()
setExitListener()
setCheckAgainListener()
}
fun showBMIResult(){
var decFormat = DecimalFormat("#.#")
var formattedBMI = decFormat.format(getIntent().getExtras().getDouble("BMIResult"))
lblBMIResult.setText(formattedBMI.toString())
}
fun findBMICategory() {
var categoryOfBMI = "Unknown"
var resultBMI = getIntent().getExtras().getDouble("BMIResult")
if (resultBMI < 15) {
categoryOfBMI = "Very Severely Underweight"
} else if (resultBMI in 15..16) {
categoryOfBMI = "Severely Underweight"
} else if (resultBMI > 16 && resultBMI <= 18.5) {
categoryOfBMI = "Underweight"
} else if (resultBMI > 18.5 && resultBMI <= 25) {
categoryOfBMI = "Normal (Healthy Weight)"
} else if (resultBMI in 25..30) {
categoryOfBMI = "Overweight"
} else if (resultBMI in 30..35) {
categoryOfBMI = "Moderately Obese"
} else if (resultBMI in 35..40) {
categoryOfBMI = "Severely Obese"
} else if (resultBMI >= 40) {
categoryOfBMI = "Very Severely Obese"
}
lblBMIResultCategory.setText(categoryOfBMI)
}
fun setExitListener() {
btnExit.setOnClickListener {
this.finishAffinity()
}
}
fun setCheckAgainListener() {
btnCheckAgain.setOnClickListener {
val intent = Intent("com.example.username.bmicalculator.MainActivity")
startActivity(intent)
}
}
}
答案 0 :(得分:0)
尝试在侦听器中调用onBackPressed()
以返回返回Main活动的按钮。
btnCheckAgain.setOnClickListener {
this.onBackPressed()
}