編輯:初階開發
練習目標
程式需要兩個 Activity:一個負責輸入兩數字,一個負責顯示結果並提供返回按鈕。因此必須在 AndroidManifest.xml 中宣告這兩個 Activity。
<?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 的命名,後續撰寫程式碼時會直接引用到。
<?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>
<?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>
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();
}
});
}
}
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、控制元件、版面檔與選單這幾個環節串起來,熟悉基本開發流程。
Android UI 佈局:LinearLayout 與 RelativeLayout
Android 介面設計中,LinearLayout 與 RelativeLayout 是最常見的兩種佈局方式。理解它們的特性,有助於建立清晰且易於維護的畫面結構。共用屬
自訂 Android DigitalClock 的時間格式
Android 的 DigitalClock 本身沒有直接提供設定時間格式的屬性,但可以透過繼承與覆寫的方式來自訂顯示內容。若只需要讓時鐘顯示小時與分鐘,可以修改原本的格
解決 Android TextView 無法顯示音標的字體難題
在開發一款小型字典應用的過程中,測試人員回報了一個看似不起眼卻相當惱人的問題:介面上的音標遲遲無法正常顯示,原本該出現的符號不是變成空白,就是顯示成一個個「豆腐字」。 排
用 Android 手機遠端控制電腦
想在被窩裡使用電腦,卻不想離開溫暖的床?透過遠端桌面,Android 手機也能成為控制電腦的終端裝置。只要電腦與手機連上同一網路,就能在手機上操作 Windows 桌面。