Android App 開發專案過程分享
http://ithelp.ithome.com.tw/ironman6/player/xq3da2/dev/1
MyNotebook 主畫面程式
處理新增筆記資料的按鈕事件。
a. 建立 MyDBHelper 物件
b. 取出組態設定的預設值
c. 執行 SQL 新增一筆筆記資料
d. 建立 intent 發出特定 Broadcast 給筆記資料的 Activity 使用
處理頁籤之間的轉換。
a. 取得系統資源物件
b. 設定個別的頁籤相關參數資料
c. 設定個別頁籤的 Intent
MyNotebook.java
package tw.brad.android.apps.MyNotebook;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TabHost;
public class MyNotebook extends TabActivity {
private ImageButton add;
private EditText newnote;
private MyDBHelper dbHelper;
private SQLiteDatabase db;
private static SharedPreferences sdata;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main();
}
private void main(){
setContentView(R.layout.main);
add = (ImageButton)findViewById(R.id.add);
newnote = (EditText)findViewById(R.id.newnote);
add.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
dbHelper = new MyDBHelper(MyNotebook.this, "brad", null, 1);
db = dbHelper.getReadableDatabase();
sdata = getSharedPreferences("Setting", MODE_PRIVATE);
int priority = sdata.getInt("priority", 2) +1;
String title = newnote.getText().toString().trim();
String status = "yet";
db.execSQL(
"INSERT INTO notebook " +
"(title,priority,del,status) VALUES " +
"('" +title+ "'," +
priority + ",'false', '" +
status + "')");
Intent intent = new Intent("tw.brad.android.apps.MyNotebook");
sendBroadcast(intent);
newnote.setText("");
}
});
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, ToDo.class);
spec = tabHost.newTabSpec(res.getString(R.string.notebook))
.setIndicator(res.getString(R.string.notebook),
res.getDrawable(R.drawable.todo))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Done.class);
spec = tabHost.newTabSpec(res.getString(R.string.garbage))
.setIndicator(res.getString(R.string.garbage),
res.getDrawable(R.drawable.done))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Config.class);
spec = tabHost.newTabSpec(res.getString(R.string.setting))
.setIndicator(res.getString(R.string.setting),
res.getDrawable(R.drawable.config))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}