編輯:關於Android編程
本文考慮把賬單界面整理下,實現如下圖中的功能。做之前感覺應該不難,但實際做時發現排列界面布局甚至比編寫程序代碼還要復雜。網上搜索發現,關於這種布局的資料能用的很少,Google Demo中用的最多的就是Listview了,但本實例的界面似乎要復雜一些。
spinner和cursor如何配合使用成了完成此實例過程中的難點,本來應該很簡單,但卻把我郁悶壞了。
先給大家貼上最終的效果圖片:
界面的xml:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"> <LinearLayout android:id="@+id/LinearLayout01" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇賬目" android:minWidth="80dip" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> <EditText android:id="@+id/edittext_acctitem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="200dip" android:maxLines="1" android:editable="false" android:cursorVisible="false"></EditText> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/LinearLayout03" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="填入費用" android:minWidth="80dip" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> <EditText android:id="@+id/Fee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numeric="decimal" android:width="160dip"></EditText> <TextView android:id="@+id/TextView13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(元)" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/LinearLayout04" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:id="@+id/TextView02" android:layout_height="wrap_content" android:text="選擇時間" android:layout_width="fill_parent" android:fadingEdge="horizontal" android:height="24dip" android:drawablePadding="2dip"></TextView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout05" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/vdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:width="120dip"></TextView> <Button android:id="@+id/BtnDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:text android:textSize="24dip" android:height="30dip" android:width="30dip"></Button> <TextView android:id="@+id/vtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:width="80dip" android:gravity="center_horizontal"></TextView> <Button android:id="@+id/BtnTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:text android:textSize="24dip"></Button> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/LinearLayout06" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="賬目類型" android:minWidth="80dip" android:textAppearance="?android:attr/textAppearanceLarge"></TextView> <Spinner android:id="@+id/Spinner01" android:layout_height="wrap_content" android:minWidth="200dip" android:layout_width="wrap_content"></Spinner> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> <TextView android:id="@+id/TextView07" android:layout_height="wrap_content" android:text="填寫備注" android:layout_width="fill_parent" android:height="24dip" ></TextView> <EditText android:id="@+id/EditTextDESC" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="4" android:gravity="top"></EditText> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> <LinearLayout android:id="@+id/LinearLayout08" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:id="@+id/BtnSave" android:width="160dip" android:text="保 存" android:text android:textSize="24dip" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:id="@+id/BtnCancel" android:width="160dip" android:text="取 消" android:text android:textSize="24dip" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> </LinearLayout> </ScrollView>
下面我們來看下spinner和cursor的用法。
主要就是一個SimpleCursorAdapter。
代碼如下:
Java代碼
s1=(Spinner) findViewById(R.id.Spinner01); String[] from= new String[]{"caption"};//需要顯示游標裡面的字段 int[] to=new int[]{android.R.id.text1}; Cursor cur=billdb.getUserid(); SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cur,from, to); mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1.setAdapter(mAdapter);
我在這兒居然搞了2天,其實寫法一直沒錯,可是每次報未知的行 _ID。這個錯誤我也知道就是使用SimpleCursorAdapter 該方法的游標裡面必須包括一個_ID的字段,可是我的表裡面肯定有的,在我重試了無數次後發現,區分大小寫,我倒!
而事實上我建表的語句是:
Java代碼
db.execSQL("Create table tusers (_id integer primary key autoincrement," + "caption text not null)");
而我在函數getUserid 裡面cursor定義是:
Java代碼
public Cursor getUserid(){ Log.v("cola","run get users cursor"); return db.query("tusers", new String[]{"_ID", "caption" }, null, null, null, null, null); }
你單獨測試這個cursor是沒有問題的。
這都沒用問題,也就是在這兒是不區分大小寫的。但是如果你用這個cursor 綁定到SimpleCursorAdapter 這個裡面去,一定要和建表語句的一致,不然就出錯。這兒把我郁悶壞了。
上面界面布局和這個spinner 搞定後,後面就是完善代碼,完善界面的功能,沒有新的地方了。
在用戶選擇完賬目,填寫費用,選擇時間,賬目類型後就保存進數據庫bills表。
附最新的代碼Frm_Addbills.java:
Java代碼
package com.cola.ui; import java.util.Calendar; import java.util.TimeZone; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.SimpleCursorAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; public class Frm_Addbills extends Activity implements OnClickListener { EditText edittext_acctitem,EditTextDESC,Fee; TextView mDate; TextView mTime; static final int RG_REQUEST = 0; private int mYear; private int mMonth; private int mDay; private int mHour; private int mMinute; Spinner s1; Button BtnDate,BtnTime; Button BtnCancel,BtnSave; BilldbHelper billdb; int acctitemid=-1; public void onCreate(Bundle icicle) { super.onCreate(icicle); setTitle("ColaBox-添加賬單"); setContentView(R.layout.frm_addbills); edittext_acctitem = (EditText)findViewById(R.id.edittext_acctitem); edittext_acctitem.setOnClickListener(this); EditTextDESC=(EditText)findViewById(R.id.EditTextDESC); Fee=(EditText)findViewById(R.id.Fee); BtnDate=(Button)findViewById(R.id.BtnDate); BtnDate.setOnClickListener(this); BtnTime=(Button)findViewById(R.id.BtnTime); BtnTime.setOnClickListener(this); BtnCancel=(Button)findViewById(R.id.BtnCancel); BtnCancel.setOnClickListener(this); BtnSave=(Button)findViewById(R.id.BtnSave); BtnSave.setOnClickListener(this); mDate = (TextView) findViewById(R.id.vdate); mTime = (TextView) findViewById(R.id.vtime); //Calendar c=Calendar.getInstance(Locale.CHINA); initTime(); setDatetime(); billdb = new BilldbHelper(this); s1=(Spinner) findViewById(R.id.Spinner01); String[] from= new String[]{"caption"}; int[] to=new int[]{android.R.id.text1}; Cursor cur=billdb.getUserid(); SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, cur,from, to); mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1.setAdapter(mAdapter); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, 1, 0, "賬目明細").setIcon(R.drawable.editbills); menu.add(0, 2, 0, "賬目統計").setIcon(R.drawable.editbills2); menu.add(0, 3, 0, "賬目報表").setIcon(R.drawable.billsum1); menu.add(0, 4, 0, "退 出").setIcon(R.drawable.quit); return true; } public void onClick(View v) { if (v.equals(edittext_acctitem)) { Log.v("ColaBox", "cmd=edittext_acctitem"); Intent intent = new Intent(); intent.setClass(Frm_Addbills.this, Frm_Editacctitem.class); startActivityForResult(intent, RG_REQUEST); } else if (v.equals(BtnTime)){ showDialog(1); } else if (v.equals(BtnDate)){ showDialog(2); } else if (v.equals(BtnCancel)){ cancel(); } else if (v.equals(BtnSave)){ save(); } } public boolean onOptionsItemSelected(MenuItem item) { //Log.v("ColaBox", "getmenuitemid=" + item.getItemId()); switch (item.getItemId()) { case 1: return true; case 2: return true; case 3: return true; case 4: QuitApp(); return true; } return false; } public void QuitApp() { new AlertDialog.Builder(Frm_Addbills.this).setTitle("提示").setMessage( "確定退出?").setIcon(R.drawable.quit).setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { billdb.close(); finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RG_REQUEST) { if (resultCode == RESULT_CANCELED) { // setTitle("Canceled..."); } else if (resultCode == RESULT_OK) { // setTitle((String)data.getCharSequenceExtra("DataKey")); edittext_acctitem.setText((String) data.getCharSequenceExtra("name")); acctitemid=Integer.parseInt((String)data.getCharSequenceExtra("id")); Log.v("cola","get acctitemid="+acctitemid); } } } private void cancel(){ Log.v("cola","u put cancel btn"); edittext_acctitem.setText(""); Fee.setText(""); acctitemid=-1; initTime();setDatetime(); EditTextDESC.setText(""); } private void save(){ Log.v("cola","u put save btn"); if (acctitemid==-1){ new AlertDialog.Builder(this) .setMessage("請首先選擇賬目.") .show(); return; } int fee=0; String s=Fee.getText().toString(); int pos=s.indexOf("."); //Log.v("cola","i="+(s.length()-pos)); if (pos>0){ if (s.length()-pos<3){ s=s+"0"; } fee=Integer.parseInt(s.substring(0,pos)+s.substring(pos+1,pos+3)); }else{ fee=Integer.parseInt(s)*100; } Log.v("cola","u put save btn"); if (billdb.Bills_save(acctitemid,fee,(int)s1.getSelectedItemId(), ((TextView)mDate).getText().toString(), ((TextView)mTime).getText().toString(),EditTextDESC.getText().toString())){ Toast.makeText(this, "保存成功.", Toast.LENGTH_SHORT).show(); cancel(); }else{ Toast.makeText(this, "保存失敗,請檢查數據.", Toast.LENGTH_SHORT).show(); } } public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: QuitApp(); return true; } return false; } private void initTime(){ Calendar c = Calendar. getInstance(TimeZone.getTimeZone("GMT+08:00")); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); } private void setDatetime(){ mDate.setText(mYear+"-"+mMonth+"-"+mDay); mTime.setText(pad(mHour)+":"+pad(mMinute)); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 1: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); case 2: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case 1: ((TimePickerDialog) dialog).updateTime(mHour, mMinute); break; case 2: ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); break; } } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; setDatetime(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; setDatetime(); } }; private static String pad(int c) { if (c >= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } }
最新的billdbhelper.java :
Java代碼
package com.cola.ui; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; /** * Provides access to a database of notes. Each note has a title, the note * itself, a creation date and a modified data. */ public class BilldbHelper { private static final String TAG = "Cola_BilldbHelper"; private static final String DATABASE_NAME = "cola.db"; SQLiteDatabase db; Context context; BilldbHelper(Context _context) { context=_context; db=context.openOrCreateDatabase(DATABASE_NAME, 0, null); Log.v(TAG,"db path="+db.getPath()); } public void CreateTable_acctitem() { try{ db.execSQL("CREATE TABLE acctitem (" + "_ID INTEGER PRIMARY KEY," + "PID integer," + "NAME TEXT" + ");"); Log.v("cola","Create Table acctitem ok"); }catch(Exception e){ Log.v("cola","Create Table acctitem err,table exists."); } } public void CreateTable_bills() { try{ db.execSQL("CREATE TABLE bills (" + "_ID INTEGER primary key autoincrement," +" acctitemid integer," + "fee integer," + "userid integer," + "sdate TEXT," + "stime TEXT," + "desc TEXT" + ");"); Log.v("cola","Create Table acctitem ok"); }catch(Exception e){ Log.v("cola","Create Table acctitem err,table exists."); } } public boolean Bills_save(int acctid,int fee,int userid,String date,String time,String text){ String sql=""; try{ sql="insert into bills values(null,"+acctid+","+fee+","+userid+",'"+date+"','"+time+"','"+text+"')"; db.execSQL(sql); Log.v("cola","insert Table bills ok"); return true; }catch(Exception e){ Log.v("cola","insert Table bills err="+sql); return false; } } public void CreateTable_colaconfig() { try{ db.execSQL("CREATE TABLE colaconfig (" + "_ID INTEGER PRIMARY KEY," + "NAME TEXT" + ");"); Log.v("cola","Create Table colaconfig ok"); }catch(Exception e){ Log.v("cola","Create Table acctitem err,table exists."); } } public void CreateTable_users() { try{ db.execSQL("Create table tusers (_id integer primary key autoincrement," + "caption text not null)"); Log.v("cola","Create Table users ok"); db.execSQL("insert into tusers values (null,'個人')"); db.execSQL("insert into tusers values (null,'公司')"); }catch(Exception e){ Log.v("cola","Create Table tusers err,table exists."); } } public void InitAcctitem() { try{ //s.getBytes(encoding); db.execSQL("insert into acctitem values (1,null,'收入')"); db.execSQL("insert into acctitem values (2,1,'工資')"); db.execSQL("insert into acctitem values (9998,1,'其他')"); db.execSQL("insert into acctitem values (0,null,'支出')"); db.execSQL("insert into acctitem values (3,0,'生活用品')"); db.execSQL("insert into acctitem values (4,0,'水電煤氣費')"); db.execSQL("insert into acctitem values (5,0,'汽油費')"); db.execSQL("insert into acctitem values (9999,0,'其他')"); //db.execSQL("insert into bills values(100,135,10000,'','','備注')"); Log.v("cola","insert into ok"); }catch(Exception e) { Log.v("cola","init acctitem e="+e.getMessage()); } } public void Acctitem_newitem(String text,int type){ Cursor c =db.query("acctitem", new String[]{"max(_id)+1"}, "_id is not null and _id<9998", null, null, null, null); c.moveToFirst(); int maxid=c.getInt(0); String sql="insert into acctitem values ("+maxid+","+type+",'"+text+"')"; db.execSQL(sql); Log.v("cola","newitem ok text="+text+" id="+type+" sql="+sql); } public void Acctitem_edititem(String text,int id){ db.execSQL("update acctitem set name='"+text+"' where _id="+id); Log.v("cola","edititem ok text="+text+" id="+id); } public void Acctitem_delitem(int id){ db.execSQL("delete from acctitem where _id="+id); Log.v("cola","delitem ok id="+id); } public void QueryTable_acctitem(){ } public void FirstStart(){ try{ String col[] = {"type", "name" }; Cursor c =db.query("sqlite_master", col, "name='colaconfig'", null, null, null, null); int n=c.getCount(); if (c.getCount()==0){ CreateTable_acctitem(); CreateTable_colaconfig(); CreateTable_bills(); CreateTable_users(); InitAcctitem(); } //test(); Log.v("cola","c.getCount="+n+""); }catch(Exception e){ Log.v("cola","e="+e.getMessage()); } } public void close(){ db.close(); } public Cursor getParentNode(){ return db.query("acctitem", new String[]{"_id", "name" }, "pid is null", null, null, null, "pid,_id"); } public Cursor getChildenNode(String pid){ Log.v("cola","run getchildenNode"); return db.query("acctitem", new String[]{"_id", "name" }, "pid="+pid, null, null, null, "_id"); } public Cursor getUserid(){ Log.v("cola","run get users cursor"); return db.query("tusers", new String[]{"_id", "caption" }, null, null, null, null, null); } public String test(){ try{ Cursor c2 =getUserid(); String ss=""; c2.moveToFirst(); while(!c2.isAfterLast()){ ss = c2.getString(0) +", "+ c2.getString(1); //byte b[]=c2.getString(1).getBytes(); c2.moveToNext(); Log.v("cola","ss="+ss+""); } return ss; }catch(Exception e){ Log.v("cola","e="+e.getMessage()); return "err"; } } }
系列文章:
Android 個人理財工具六:顯示賬單明細 下
Android 個人理財工具五:顯示賬單明細 上
Android 個人理財工具四:添加賬單頁面 下
Android 個人理財工具三:添加賬單頁面 上
Android 個人理財工具二:使用SQLite實現啟動時初始化數據
Android 個人理財工具一:項目概述與啟動界面的實現
以上就Android 理財工具詳情頁面的開發,後續繼續補充,其他功能,謝謝大家對本站的支持!
一、前言現在主流的加固平台有:梆梆加固,愛加密,360加固,騰訊加固,在之前的一篇文章中介紹了:如何脫掉“愛加密”的殼,現在這裡要脫掉另外一個平台
一、前言作為一個android開發者,使用xml寫UI,實在是太方便了。最近學習Weex,需要使用css來布局。學成之後,發現使用CSS的Flex布局樣式也非常方便。在c
效果圖: scrollview+listView 疊加嵌套實現 禁止滑動ListView : 2,
本文均屬自己閱讀源碼的點滴總結,轉賬請注明出處謝謝。歡迎和大家交流。qq:1037701636 email:[email protected]:系統