2016年7月20日 星期三

activity 和 activity 互傳資料...可以使用bundle

先看 AndroidManifest.xml

用到幾個activity 就要全部宣告在這個檔案裡面

.MainActivity

.ResultActivity


 android:label="@string/title_main_activity" 這個會秀在actionbar上面

<category android:name="android.intent.category.LAUNCHER" /> // 應用程式安裝完會自動啟動

<action android:name="android.intent.action.MAIN" /> // 設定此Activity 頁面為首頁

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="idv.ron.activitiesdemo" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"    //  指定app 的icon image
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_main_activity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" /> // 應用程式安裝完會自動啟動
            </intent-filter>
        </activity>
        <activity
            android:name=".ResultActivity"
            android:label="@string/title_result_activity" >
        </activity>
    </application>

</manifest>


接下來在看來    main_activity.xml


其實就是3個 EditText 讓使用者輸入成績和一個按鈕可以呼叫onSubmitClick()切換倒下一頁



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_tvTitle"
        android:id="@+id/textView"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/etProgramming"
        android:layout_below="@+id/textView"
        android:layout_marginTop="42dp"
        android:hint="@string/hint_etProgramming" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/etDataStructure"
        android:layout_below="@+id/etProgramming"
        android:hint="@string/hint_etDataStructure" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/etAlgorithm"
        android:layout_below="@+id/etDataStructure"
        android:hint="@string/hint_etAlgorithm" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_btSubmit"
        android:id="@+id/btSubmit"
        android:layout_below="@+id/etAlgorithm"
        android:onClick="onSubmitClick" />
</RelativeLayout>


先來看  MainActivity.java

package idv.ron.activitiesdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText etProgramming, etDataStructure, etAlgorithm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        findViews();
    }

    private void findViews() {
        etProgramming = (EditText) findViewById(R.id.etProgramming);
        etDataStructure = (EditText) findViewById(R.id.etDataStructure);
        etAlgorithm = (EditText) findViewById(R.id.etAlgorithm);
    }

    private boolean isValid(EditText editText) {
        String pattern = "1[0]{2}|[0-9]{1,2}";
        String text = editText.getText().toString();
        if (!text.matches(pattern)) {
            editText.setError("0 ~ 100");
            return false;
        } else {
            return true;
        }
    }

//  按下button 會呼叫的函式

    使用一個Bundle ...把需要傳遞過去的東西加到Bundle裡面

   最後再呼叫 intent.putExtras(bundle);  把bundle 加到 intent裡面...

    public void onSubmitClick(View view) {
        // use "&" not "&&" because even the first isValid() return false,
        // the second isValid() will still be called
        boolean isValid =
                isValid(etProgramming) & isValid(etDataStructure) & isValid(etAlgorithm);
        if (!isValid) {
            return;
        }
        int programming = Integer.parseInt(etProgramming.getText().toString());
        int dataStructure = Integer.parseInt(etDataStructure.getText().toString());
        int algorithm = Integer.parseInt(etAlgorithm.getText().toString());
        Intent intent = new Intent(this, ResultActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("programming", programming);
        bundle.putInt("dataStructure", dataStructure);
        bundle.putInt("algorithm", algorithm);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}


再來看result_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="idv.ron.activitiesdemo.ResultActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_tvResultTitle"
        android:id="@+id/tvResultTitle"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvResult"
        android:textSize="16sp"
        android:layout_marginTop="12dp"
        android:lines="5"
        android:layout_below="@+id/tvResultTitle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_btBack"
        android:id="@+id/btBack"
        android:onClick="onBackClick"
        android:layout_below="@+id/tvResult"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>


再來看ResultActivity.java

package idv.ron.activitiesdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

public class ResultActivity extends AppCompatActivity {
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_activity);
        tvResult = (TextView) findViewById(R.id.tvResult);
        showResults();
    }

    private void showResults() {
        NumberFormat nf = NumberFormat.getInstance();
        Bundle bundle = getIntent().getExtras();
        int programming = bundle.getInt("programming");
        int dataStructure = bundle.getInt("dataStructure");
        int algorithm = bundle.getInt("algorithm");
        int sum = programming + dataStructure + algorithm;
        double average = sum / 3.0;
        String text = "programming = " + programming +
                "\ndataStructure = " + dataStructure +
                "\nalgorithm = " + algorithm +
                "\nsum = " + sum +
                "\naverage = " + nf.format(average);
        tvResult.setText(text);
    }

    public void onBackClick(View view) {
        finish();   // 呼叫 finish() 可以結束目前Activity的頁面.....
    }
}




沒有留言:

張貼留言