打地鼠1.0版本已经完成,算一期工程完成了吧。0.9版本实现了数据的存储,1.0版本做了不少测试,修复了一些bug,优化了一下代码。
代码多,贴出来太占地方了,就把和0.9版本相关的数据库方面的文件贴一下。
DBHelper.java
package com.fsy.hitmouse; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * 管理记录数据库 * @author Fsy * */ public class DBHelper extends SQLiteOpenHelper{ private static int VERSION = 1; public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public DBHelper(Context context, String name, int version) { this(context, name,null, version); } public DBHelper(Context context, String name) { this(context, name,VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table winner(name varchar(20),score int)"); Manage.DB_flag = true; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
InputActivity.java
package com.fsy.hitmouse; import android.app.Activity; import android.content.ContentValues; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; /** * 输入玩家信息的Activity * @author Fsy * */ public class InputActivity extends Activity{ Button b; EditText editText; String name; boolean flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.input); setTitle(" 给力啊 果然犀利~ "); editText = (EditText)findViewById(R.id.winner_name); b = (Button)findViewById(R.id.button_yes); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { flag = false; if (!(name=editText.getText().toString()).equals("")) { ContentValues values = new ContentValues(); values.put("name", name); values.put("score", Manage.score); Manage.activitymain.dbwrite.insert("winner", null, values); flag = true; } if(flag){ finish(); } } }); } }
ListScoreActivity.java
package com.fsy.hitmouse; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; /** * 列出排名的Activity * @author Fsy * */ public class ListScoreActivity extends Activity{ /** * 排名,共5个 */ TextView t[]; Button b; String name; String score; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("打地鼠英雄榜"); setContentView(R.layout.list_score); t = new TextView[5]; t[0]= (TextView)findViewById(R.id.text1); t[1]= (TextView)findViewById(R.id.text2); t[2]= (TextView)findViewById(R.id.text3); t[3]= (TextView)findViewById(R.id.text4); t[4]= (TextView)findViewById(R.id.text5); Cursor cursor = Manage.activitymain.dbread.query("winner", null, null, null, null, null, "score desc"); int temp=0; while(cursor.moveToNext()){ name = cursor.getString(cursor.getColumnIndex("name")); score = cursor.getString(cursor.getColumnIndex("score")); if(temp>4) break; t[temp].setText(name+" "+score); temp++; } b= (Button)findViewById(R.id.button_list); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
ScoreActivity.java
package com.fsy.hitmouse; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; /** * 未破纪录的玩家分数Activity * @author Fsy * */ public class ScoreActivity extends Activity{ TextView text; Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("还差一点,没破记录"); setContentView(R.layout.score); text = (TextView)findViewById(R.id.text_show_score); text.setText("你的分数为: "+Manage.score); b= (Button)findViewById(R.id.button_show_score); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
TimeHandler.java
package com.fsy.hitmouse; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.os.Handler; import android.os.Looper; import android.os.Message; /** * 管理进度条更新的Handler * @author Fsy * */ public class TimeHandler extends Handler{ Intent t; public TimeHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { Manage.activitymain.progressbar.setProgress(Manage.passed_time); //处理第一次创建数据库时表为空的情况,应该还有更好的解决方法? if(Manage.DB_flag){ ContentValues values = new ContentValues(); values.put("name", "fsy"); values.put("score", 0); Manage.activitymain.dbwrite.insert("winner", null, values); Manage.DB_flag = false; } if (Manage.passed_time>=100) { Manage.make_mouse = false; Cursor cursor = Manage.activitymain.dbread.query("winner", null, null, null, null, null, "score desc"); int counter=0; while(cursor.moveToNext()){ if(counter == 5){ break; } if (Manage.score >cursor.getInt(cursor.getColumnIndex("score"))) { t = new Intent(); t.setClass(Manage.activitymain, InputActivity.class); Manage.activitymain.startActivity(t); break; } counter++; } if(counter == 5){ t = new Intent(); t.setClass(Manage.activitymain, ScoreActivity.class); Manage.activitymain.startActivity(t); } } } }