Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Android 簡易計算機實作(一):Intent 傳值與選單

編輯:初階開發

練習目標

  1. 熟悉 Intent 的使用方式
  2. 熟悉常用語法
  3. 走一遍 Android 開發的完整流程
  4. 掌握 Options Menu 的用法

整體設計

程式需要兩個 Activity:一個負責輸入兩數字,一個負責顯示結果並提供返回按鈕。因此必須在 AndroidManifest.xml 中宣告這兩個 Activity。

步驟一:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="me.Calculator"
    android:versionCode="1"
    android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="result" android:label="@string/app_name" />
    </application>

    <uses-sdk android:minSdkVersion="7" />
</manifest>

這裡要特別注意 Activity 的命名,後續撰寫程式碼時會直接引用到。

步驟二:兩個 Activity 的版面

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/APPTitle" />

    <EditText
        android:id="@+Id/edtChengshu1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+Id/tvSign"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sign" />

    <EditText
        android:id="@+Id/edtChengShu2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+Id/btnCalu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/calu" />
</LinearLayout>

resultframe.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+Id/tvResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Result" />

    <Button
        android:id="@+id/btnfh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Return" />
</LinearLayout>

步驟三:撰寫程式碼

Main.java

package me.Calculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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;

public class Main extends Activity {

    Button Btncalu1;
    EditText edt11;
    EditText edt21;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 1, "退出");
        menu.add(0, 2, 2, "關於");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == 1) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        edt11 = (EditText) findViewById(R.Id.edtChengshu1);
        edt21 = (EditText) findViewById(R.Id.edtChengShu2);
        Btncalu1 = (Button) findViewById(R.Id.btnCalu);

        Btncalu1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String chengshu1 = edt11.getText().toString();
                String chengshu2 = edt21.getText().toString();

                Intent intent = new Intent();
                intent.putExtra("chengshu1", chengshu1);
                intent.putExtra("chengshu2", chengshu2);
                intent.setClass(Main.this, result.class);
                Main.this.startActivity(intent);
                Main.this.finish();
            }
        });
    }
}

result.java

package me.Calculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class result extends Activity {

    TextView tv;
    Button btn;
    String chengshu1, chengshu2;

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

        Intent intent = getIntent();
        chengshu1 = intent.getExtras().getString("chengshu1");
        chengshu2 = intent.getExtras().getString("chengshu2");

        int a = Integer.parseInt(chengshu1);
        int b = Integer.parseInt(chengshu2);
        int c = a * b;

        tv = (TextView) findViewById(R.Id.tvResult);
        tv.setText(chengshu1 + " 乘以 " + chengshu2 + " = " + String.valueOf(c));

        btn = (Button) findViewById(R.id.btnfh);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(result.this, Main.class);
                result.this.startActivity(intent);
                result.this.finish();
            }
        });
    }
}

幾個要注意的地方

類別名稱要一致。Java 原始檔的名稱最好與 AndroidManifest.xml 中 Activity 的宣告保持一致,否則編譯容易出問題。

選單的 id 判斷。我在 onCreateOptionsMenu 中用 menu.add(0, 1, 1, "退出") 把項目的 itemId 設為 1,因此 onOptionsItemSelected 裡要判斷 item.getItemId() == 1。原始範例寫成 0 是個筆誤,這裡一併修正。

Intent 傳值的節奏。Main 用 putExtra 把兩個數字塞進 Intent,再啟動 result;result 用 getExtras().getString() 取出。這是最基礎的 Activity 間通訊方式,後續若資料量大,才需要改用 Bundle、Parcelable 或跨程序的方式。

程式邏輯本身很簡單,重點在於把 Intent、控制元件、版面檔與選單這幾個環節串起來,熟悉基本開發流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved