延續上面的例子,只需加上及修改一些內容即可
strings.xml加入
<string name="advice_history">上次測試結果:</string>
<string name="input_empty"></string>
MainActivity.java修改及加入
加入變數
private static final int ACTIVITY_REPORT = 1000;
onClcik(view v)修改
//startActivity(intent);
startActivityForResult(intent, ACTIVITY_REPORT);
新增onActivityResult事件
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode == RESULT_OK)
    {
        if(requestCode == ACTIVITY_REPORT)
        {
            Bundle bundle = intent.getExtras();
            String bmi = bundle.getString("BMI");
            show_suggest.setText(getString(R.string.advice_history) + bmi);
            num_weight.setText(R.string.input_empty);
            num_weight.requestFocus();
        }
    }
}
ReportActivity.java修改
宣告變數
private double BMI;
showResults修改
BMI = weight / (height*height);
backtoMain修改
private Button.OnClickListener backtoMain = new Button.OnClickListener()
{
    public void onClick(View v)
    {
        DecimalFormat nf = new DecimalFormat("0.00");
        Bundle bundle = new Bundle();
        bundle.putString("BMI", nf.format(BMI));
        Intent intent = new Intent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        ReportActivity.this.finish();
    }
};
前面的畫面跟之前一樣,以下是返回後的畫面
以下是完整程式碼
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/bmi_hello" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_height" />
        <EditText
            android:id="@+id/height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_weight" />
        <EditText
            android:id="@+id/weight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_btn" />
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
        <TextView
            android:id="@+id/suggest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.user.mybmi;
import android.content.Intent;
import android.os.LocaleList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
    private static final int ACTIVITY_REPORT = 1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.w("Main", "開始事件");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //取得控制項物件
        initViews();
        //設定監聽事件
        setListensers();
    }
    private Button button_calc;
    private EditText num_height;
    private EditText num_weight;
    private TextView show_result;
    private TextView show_suggest;
    //取得控制項物件
    private void initViews()
    {
        button_calc = (Button)findViewById(R.id.button);
        num_height = (EditText)findViewById(R.id.height);
        num_weight = (EditText)findViewById(R.id.weight);
        show_result = (TextView)findViewById(R.id.result);
        show_suggest = (TextView)findViewById(R.id.suggest);
    }
    //設定監聽事件
    private void setListensers()
    {
        button_calc.setOnClickListener(calcBMI);
    }
    private OnClickListener calcBMI = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ReportActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("KEY_HEIGHT", num_height.getText().toString());
            bundle.putString("KEY_WEIGHT", num_weight.getText().toString());
            intent.putExtras(bundle);
            startActivityForResult(intent, ACTIVITY_REPORT);
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
        if(resultCode == RESULT_OK)
        {
            if(requestCode == ACTIVITY_REPORT)
            {
                Bundle bundle = intent.getExtras();
                String bmi = bundle.getString("BMI");
                show_suggest.setText(getString(R.string.advice_history) + bmi);
                num_weight.setText(R.string.input_empty);
                num_weight.requestFocus();
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id)
        {
            case R.id.action_settings:
                openOptionsDialog();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    private void openOptionsDialog()
    {
        Toast popup = Toast.makeText(this, "BMI計算機", Toast.LENGTH_SHORT);
        popup.show();
    }
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BMI</string>
    <string name="bmi_hello">哈囉!BMI</string>
    <string name="bmi_height">身高 (cm)</string>
    <string name="bmi_weight">體重 (kg)</string>
    <string name="bmi_btn">計算BMI值</string>
    <string name="bmi_result">你的BMI值是</string>
    <string name="advice_light">你該多吃點</string>
    <string name="advice_average">體型很棒喔!</string>
    <string name="advice_heavy">你該節食了</string>
    <string name="action_settings">設定</string>
    <string name="report_title">BMI報告</string>
    <string name="report_back">前一頁</string>
    <string name="advice_history">上次測試結果:</string>
    <string name="input_empty"></string>
</resources>
activity_report.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".ReportActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/report_title" />
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
        <TextView
            android:id="@+id/suggest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/report_back" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
ReportActivity.java
package com.example.user.mybmi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class ReportActivity extends AppCompatActivity {
    private double BMI;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        initViews();
        showResults();
        setListensers();
    }
    private Button button_back;
    private TextView show_result;
    private TextView show_suggest;
    private void initViews()
    {
        button_back = (Button)findViewById(R.id.button);
        show_result = (TextView)findViewById(R.id.result);
        show_suggest = (TextView)findViewById(R.id.suggest);
    }
    private void showResults()
    {
        try
        {
            DecimalFormat nf = new DecimalFormat("0.00");
            Bundle bundle = this.getIntent().getExtras();
            //身高
            double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
            //體重
            double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
            //計算出BMI值
            BMI = weight / (height*height);
            //結果
            show_result.setText(getText(R.string.bmi_result) + nf.format(BMI));
            //建議
            if(BMI > 25) //太重了
                show_suggest.setText(R.string.advice_heavy);
            else if(BMI < 20) //太輕了
                show_suggest.setText(R.string.advice_light);
            else //剛剛好
                show_suggest.setText(R.string.advice_average);
        }
        catch(Exception obj)
        {
            Toast.makeText(this, "要先輸入身高體重喔!", Toast.LENGTH_SHORT).show();
        }
    }
    private void setListensers()
    {
        button_back.setOnClickListener(backtoMain);
    }
    private Button.OnClickListener backtoMain = new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            DecimalFormat nf = new DecimalFormat("0.00");
            Bundle bundle = new Bundle();
            bundle.putString("BMI", nf.format(BMI));
            Intent intent = new Intent();
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            ReportActivity.this.finish();
        }
    };
}
大大,想請問一下,我這個部份沒有對應到該怎麼處理。
上次問題我自己後來摸索後解決了,目前卡在這邊就完成了
麻煩您了,感謝
你的圖看不清楚,
不過strings.xml會出現紅色,
應該是你的名稱重複的吧,
名稱不能夠重複,
而且應該只能英文開頭,
內容必須是英文數字底線等,
某些特殊字元應該不行。
謝謝大大,我後來研究完也處理好嚕:)
目前已經完成了 感激