你的位置:首页 > 操作系统

[操作系统]完整项目:木子记事本(原创,转载请注明出处)。


 

一个较为完整的记事本App,记录,存储到数据库,编辑,保存。

运行效果图:

Activity类:

欢迎界面:

 1 package com.lixu.jishiben; 2  3 import com.gitonway.lee.niftynotification.lib.Effects; 4 import com.lixu.jishiben.R; 5 import com.lixu.jishiben.draws.Mydrawable; 6 import com.lixu.jishiben.untils.Untils; 7  8 import android.app.Activity; 9 import android.content.Intent; 10 import android.content.SharedPreferences; 11 import android.content.SharedPreferences.Editor; 12 import android.os.Bundle; 13 import android.support.v4.app.Fragment; 14 import android.support.v4.app.FragmentActivity; 15 import android.support.v4.app.FragmentManager; 16 import android.support.v4.app.FragmentPagerAdapter; 17 import android.support.v4.view.ViewPager; 18 import android.support.v4.view.ViewPager.OnPageChangeListener; 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.View.OnClickListener; 22 import android.view.ViewGroup; 23 import android.view.Window; 24 import android.widget.Button; 25 import android.widget.ImageView; 26  27 public class WelcomeActivity extends FragmentActivity { 28   private static final String NAME = "name"; 29   private Mydrawable mMydrawable; 30   private Activity activity; 31   private String APP_FIRST_RUN = "app_first_run"; 32   private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3, }; 33   private Effects effect; 34   // 三种弹出动画效果 35   private Effects[] eff = { Effects.jelly, Effects.slideIn, Effects.slideOnTop }; 36  37   private String[] xiaoxin = { "欢迎使用木子记事本,我是蜡笔小新,你的私人助理哦。。。。么么哒!", "希望你经常记录你的私人笔记哦!", "现在点击进入就可以开始使用了哦,我会随时在你身边!" }; 38  39   @SuppressWarnings("deprecation") 40   @Override 41   protected void onCreate(Bundle savedInstanceState) { 42     activity = this; 43     super.onCreate(savedInstanceState); 44  45     SharedPreferences mSharedPreferences = this.getSharedPreferences(NAME, this.MODE_PRIVATE); 46     boolean first_run = mSharedPreferences.getBoolean(APP_FIRST_RUN, true); 47     // 第一次进入后修改成false 以后进入就不会重新进入欢迎界面了 48     if (first_run == true) { 49       Editor e = mSharedPreferences.edit(); 50       e.putBoolean(APP_FIRST_RUN, false); 51       e.commit(); 52       // 进入主程序关闭初始动画界面 53       this.finish(); 54     } else { 55       Intent intent = new Intent(this, MainActivity.class); 56       startActivity(intent); 57       this.finish(); 58     } 59  60     // 让程序全屏 61     requestWindowFeature(Window.FEATURE_NO_TITLE); 62     Untils.toggleFullscreen(activity, true); 63  64     setContentView(R.layout.welcome); 65  66     mMydrawable = (Mydrawable) findViewById(R.id.mydrawable); 67     // 根据情况设置圆球数量 68     mMydrawable.setCount(images.length); 69  70     ViewPager vp = (ViewPager) findViewById(R.id.vp); 71  72     vp.setAdapter(new MyAdapter(this.getSupportFragmentManager())); 73  74     vp.setOnPageChangeListener(new OnPageChangeListener() { 75       // 设置页面滑动时改变红点的位置 76       @Override 77       public void onPageSelected(int pos) { 78  79         mMydrawable.choose(pos); 80  81         NotifiActivity nba = new NotifiActivity(activity, xiaoxin[pos], eff[pos], R.layout.welcome_first, 82             R.drawable.dfdf); 83         nba.show(); 84  85       } 86  87       @Override 88       public void onPageScrolled(int arg0, float arg1, int arg2) { 89  90       } 91  92       @Override 93       public void onPageScrollStateChanged(int arg0) { 94  95       } 96     }); 97     // 初始弹出对话框 98     effect = Effects.flip; 99     NotifiActivity nba = new NotifiActivity(activity, xiaoxin[0], effect, R.layout.welcome_first, R.drawable.dfdf);100     nba.show();101 102   }103 104   private class MyAdapter extends FragmentPagerAdapter {105 106     public MyAdapter(FragmentManager fm) {107       super(fm);108     }109 110     @Override111     public Fragment getItem(final int pos) {112 113       Fragment f = new Fragment() {114         @Override115         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {116 117           if (pos == (getCount() - 1)) {118 119             View view = inflater.inflate(R.layout.welcome_end, null);120             Button btn = (Button) view.findViewById(R.id.btn);121             btn.setOnClickListener(new OnClickListener() {122 123               @Override124               public void onClick(View v) {125                 Intent intent = new Intent(activity, MainActivity.class);126                 startActivity(intent);127                 // 进入主程序关闭初始动画界面128                 activity.finish();129               }130             });131             ImageView iv = (ImageView) view.findViewById(R.id.iv_end);132             iv.setImageResource(images[pos]);133 134             return view;135 136           } else {137             View view = inflater.inflate(R.layout.welcome_first, null);138 139             ImageView iv = (ImageView) view.findViewById(R.id.iv_first);140             iv.setImageResource(images[pos]);141 142             return view;143 144           }145         }146       };147 148       return f;149     }150 151     @Override152     public int getCount() {153       return images.length;154     }155 156   }157 158 }

主界面:

 1 package com.lixu.jishiben; 2  3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7  8 import com.gitonway.lee.niftynotification.lib.Effects; 9 import com.j256.ormlite.dao.Dao; 10 import com.lixu.jishiben.R; 11 import com.lixu.jishiben.database.Data; 12 import com.lixu.jishiben.database.ORMLiteDatabaseHelper; 13 import com.lixu.jishiben.untils.Untils; 14  15 import android.app.Activity; 16 import android.app.AlertDialog; 17 import android.content.Context; 18 import android.content.DialogInterface; 19 import android.content.Intent; 20 import android.content.SharedPreferences; 21 import android.os.AsyncTask; 22 import android.os.Bundle; 23 import android.preference.PreferenceManager; 24 import android.view.ContextMenu; 25 import android.view.KeyEvent; 26 import android.view.LayoutInflater; 27 import android.view.Menu; 28 import android.view.MenuItem; 29 import android.view.View; 30 import android.view.View.OnClickListener; 31 import android.view.ViewGroup; 32 import android.view.ContextMenu.ContextMenuInfo; 33 import android.widget.AdapterView; 34 import android.widget.AdapterView.AdapterContextMenuInfo; 35 import android.widget.AdapterView.OnItemClickListener; 36 import android.widget.ArrayAdapter; 37 import android.widget.ImageView; 38 import android.widget.ListView; 39 import android.widget.PopupMenu; 40 import android.widget.TextView; 41 import android.widget.Toast; 42  43 public class MainActivity extends Activity { 44   private Activity activity; 45   private ListView lv; 46   private MyAdapter mMyAdapter; 47   private ArrayList<HashMap<String, Object>> list; 48   // 设置默认字体值 49   private float size = 15.0f; 50  51   // 设置第三方开源框架吐丝框的风格 52   private Effects effect; 53  54   @Override 55   protected void onCreate(Bundle savedInstanceState) { 56     super.onCreate(savedInstanceState); 57     activity = this; 58     // 设置全屏 59     Untils.toggleFullscreen(activity, true); 60     setContentView(R.layout.main_activity); 61  62     list = new ArrayList<HashMap<String, Object>>(); 63  64     lv = (ListView) findViewById(R.id.listview); 65     mMyAdapter = new MyAdapter(this, -1); 66     lv.setAdapter(mMyAdapter); 67     // 设置listview长按点击菜单 68     lv.setOnCreateContextMenuListener(this); 69  70     lv.setOnItemClickListener(new OnItemClickListener() { 71  72       @Override 73       public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { 74         // 根据下标获取到数据库中对应数据的id,放入intent中传递到NoteBookActivity 75         int text_id = (Integer) list.get(pos).get(Data.USER_ID); 76  77         Intent intent = new Intent(activity, NoteBookActivity.class); 78         intent.putExtra(Data.USER_ID, text_id); 79         startActivity(intent); 80       } 81     }); 82  83     final ImageView iv = (ImageView) findViewById(R.id.iv1); 84     iv.setOnClickListener(new OnClickListener() { 85  86       @Override 87       public void onClick(View v) { 88         showPopupMenu(iv); 89       } 90     }); 91     // 设置复用 92     findViewById(R.id.iv2).setVisibility(View.GONE); 93  94   } 95  96   // 为防止阻塞主线程,耗时操作放入AsyncTask中处理 97   @Override 98   protected void onResume() { 99     new MyAsyncTask().execute();100     super.onResume();101   }102 103   private void showPopupMenu(View view) {104     // View当前PopupMenu显示的相对View的位置105     PopupMenu popupMenu = new PopupMenu(this, view);106 107     // menu布局108     popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());109 110     // menu的item点击事件111     popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {112       @Override113       public boolean onMenuItemClick(MenuItem item) {114         switch (item.getItemId()) {115         case R.id.new_text:116           Intent intent = new Intent(activity, NoteBookActivity.class);117           // 故意设置一个值为-1118           intent.putExtra(Data.USER_ID, -1);119           startActivity(intent);120           break;121 122         case R.id.jishiben_setting:123           // 这里需要启动一个中转的Activity来启动SettingFragment124           Intent intent1 = new Intent(activity, ActivitySetting.class);125           startActivity(intent1);126 127           break;128 129         case R.id.exit:130           // 程序activity全部退出131           System.exit(0);132           break;133 134         default:135           break;136         }137 138         return false;139       }140     });141 142     popupMenu.show();143   }144 145   // 异步任务,处理耗时操作146   private class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, Object>>> {147     @Override148     protected void onPreExecute() {149       super.onPreExecute();150     }151 152     @Override153     protected ArrayList<HashMap<String, Object>> doInBackground(String... params) {154 155       return GetDataFromDB();156 157     }158 159     @Override160     protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {161       // 获取设置的字体大小值162       SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);163       String s = sharedpreferences.getString("edit_text_key_1", "15");164       size = Float.parseFloat(s);165       // 返回运行状态时,清空list,再从数据库读取数据,避免重复添加到list中166       list.clear();167 168       for (HashMap<String, Object> n : result)169         list.add(n);170       // 刷新适配器171       mMyAdapter.notifyDataSetChanged();172     }173 174   }175 176   // 创建界面长按点击弹出菜单的点击事件177   @Override178   public boolean onContextItemSelected(MenuItem item) {179     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();180     // 获取点击哪条数据的下标181     final int pos = info.position;182     // 根据下标获取到数据库中对应数据的id183     final int text_now_id = (Integer) list.get(pos).get(Data.USER_ID);184 185     switch (item.getItemId()) {186     case 1001:187       zhiding(pos, text_now_id);188 189       effect = Effects.thumbSlider;190       NotifiActivity nba = new NotifiActivity(activity, "已置顶!", effect, R.id.main, R.drawable.dfdf);191       nba.show();192       break;193 194     case 1002:195       // 对话提示选择框196       AlertDialog dialog1 = new AlertDialog.Builder(this).create();197       dialog1.setTitle("确定要删除吗?");198       dialog1.setIcon(R.drawable.delete);199       dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {200 201         @SuppressWarnings("unchecked")202         @Override203         public void onClick(DialogInterface dialog, int which) {204           deleteData(pos, text_now_id);205         }206       });207 208       dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {209 210         @Override211         public void onClick(DialogInterface dialog, int which) {212 213           effect = Effects.flip;214           NotifiActivity nba = new NotifiActivity(activity, "已取消!", effect, R.id.main, R.drawable.dfdf);215           nba.show();216         }217       });218       dialog1.show();219 220       break;221 222     default:223       break;224     }225 226     return super.onContextItemSelected(item);227   }228 229   // 获取手机返回按钮,返回完全退出程序230   @Override231   public boolean onKeyDown(int keyCode, KeyEvent event) {232     if (keyCode == KeyEvent.KEYCODE_BACK) {233       System.exit(0);234     }235 236     return false;237   }238 239   private void zhiding(int pos, int id) {240     // arraylist数据置顶241     HashMap<String, Object> map = new HashMap<String, Object>();242     map = list.get(pos);243     // 修改map中的时间244     map.remove(Data.SYSTEM_TIME);245     map.put(Data.SYSTEM_TIME, Untils.formatTimeInMillis(System.currentTimeMillis()));246     list.remove(pos);247     list.add(0, map);248     // 数据库数据置顶249     Dao dao = GetDao();250     try {251       Data d = (Data) dao.queryForId(id);252       // 置顶后改变修改时间253       d.setTime_text(Untils.formatTimeInMillis(System.currentTimeMillis()));254       dao.createOrUpdate(d);255 256     } catch (SQLException e) {257       e.printStackTrace();258     }259     mMyAdapter.notifyDataSetChanged();260 261   }262 263   private void deleteData(int pos, int id) {264     Dao dao = GetDao();265 266     try {267       // 删除数据库中的数据268       dao.deleteById(id);269 270     } catch (SQLException e) {271       e.printStackTrace();272     }273     // 删除arraylist中的数据274     list.remove(pos);275 276     mMyAdapter.notifyDataSetChanged();277 278     effect = Effects.jelly;279     NotifiActivity nba = new NotifiActivity(activity, "已删除!", effect, R.id.main, R.drawable.dfdf);280     nba.show();281   }282 283   // 创建界面长按点击弹出菜单284   @Override285   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {286     super.onCreateContextMenu(menu, v, menuInfo);287 288     menu.add(0, 1001, 101, "置顶此文本");289     menu.add(0, 1002, 102, "删除此文本");290 291   }292 293   // 通过ORM数据库工具找到Dao工具类294   private Dao GetDao() {295 296     ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(this);297     Dao dataDao = mDatabaseHelper.getUserDao();298 299     return dataDao;300   }301   // 从数据库获取数据302 303   private ArrayList<HashMap<String, Object>> GetDataFromDB() {304     // 设置缓存list305     ArrayList<HashMap<String, Object>> list_cache = new ArrayList<HashMap<String, Object>>();306     Dao dataDao = GetDao();307 308     try {309       // 将数据库内容按逆序(按更新时间)排列,使得每次添加新文本到最顶部310       // list.add(0,map)这样也可以实现,但是如果数据量大了以后对性能影响很大311       List<Data> datas = dataDao.queryBuilder().orderBy(Data.SYSTEM_TIME, false).query();312 313       for (Data d : datas) {314         HashMap<String, Object> map = new HashMap<String, Object>();315         map.put(Data.TEXT, d.getText());316         map.put(Data.TITLE, d.getTitle());317         map.put(Data.SYSTEM_TIME, d.getTime_text());318         map.put(Data.USER_ID, d.user_id);319 320         list_cache.add(map);321 322       }323 324     } catch (SQLException e) {325       e.printStackTrace();326     }327     return list_cache;328 329   }330 331   private class MyAdapter extends ArrayAdapter<String> {332     private LayoutInflater flater;333 334     public MyAdapter(Context context, int resource) {335       super(context, resource);336       flater = LayoutInflater.from(context);337     }338 339     @Override340     public int getCount() {341       return list.size();342     }343 344     @Override345     public View getView(int position, View convertView, ViewGroup parent) {346       if (convertView == null)347         convertView = flater.inflate(R.layout.listview_layout, null);348 349       TextView tv_biaoti = (TextView) convertView.findViewById(R.id.biaoti);350 351       tv_biaoti.setTextSize(size);352       TextView tv_time = (TextView) convertView.findViewById(R.id.time);353 354       tv_biaoti.setText(list.get(position).get(Data.TITLE).toString());355       tv_time.setText(list.get(position).get(Data.SYSTEM_TIME).toString());356 357       return convertView;358     }359 360   }361 362 }

编辑界面:

 1 package com.lixu.jishiben; 2  3 import java.sql.SQLException; 4  5 import com.gitonway.lee.niftynotification.lib.Effects; 6 import com.j256.ormlite.dao.Dao; 7 import com.lixu.jishiben.R; 8 import com.lixu.jishiben.database.Data; 9 import com.lixu.jishiben.database.ORMLiteDatabaseHelper; 10 import com.lixu.jishiben.untils.Untils; 11  12 import android.app.Activity; 13 import android.app.AlertDialog; 14 import android.content.DialogInterface; 15 import android.content.Intent; 16 import android.content.SharedPreferences; 17 import android.os.Bundle; 18 import android.preference.PreferenceManager; 19 import android.view.KeyEvent; 20 import android.view.MenuItem; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.widget.EditText; 24 import android.widget.ImageView; 25 import android.widget.PopupMenu; 26 import android.widget.TextView; 27  28 public class NoteBookActivity extends Activity { 29   private Activity activity; 30   private int SAVE_ID = 1987; 31  32   private EditText title_text; 33   private EditText text_text; 34  35   private Dao<Data, Integer> dataDao; 36  37   private Data mdata; 38  39   // 设置第三方开源框架吐丝框的风格 40   private Effects effect; 41  42   // 设置文本字体大小初始值 43   private float size = 12.0f; 44  45   @SuppressWarnings("unchecked") 46   @Override 47   protected void onCreate(Bundle savedInstanceState) { 48     super.onCreate(savedInstanceState); 49  50     activity = this; 51     // 设置全屏 52     Untils.toggleFullscreen(activity, true); 53     setContentView(R.layout.notebook); 54  55     title_text = (EditText) findViewById(R.id.title); 56     text_text = (EditText) findViewById(R.id.text); 57  58     // 从intent中获取MainActivity传来的数据 59     Intent intent = getIntent(); 60     int text_id = intent.getIntExtra(Data.USER_ID, -1); 61     // 如果进入新建 文本则为空 62     String title1 = "", text1 = ""; 63  64     if (text_id != -1) { 65       // 从listview中点击进入 66       dataDao = GetDao(); 67       try { 68         mdata = dataDao.queryForId(text_id); 69       } catch (SQLException e) { 70         e.printStackTrace(); 71       } 72       title1 = mdata.getTitle(); 73       text1 = mdata.getText(); 74  75     } else { 76       // 从新建文本进入 77       // 实例化 78       mdata = new Data(); 79     } 80  81     title_text.setText(title1); 82     text_text.setText(text1); 83  84     final ImageView iv = (ImageView) findViewById(R.id.iv1); 85     iv.setOnClickListener(new OnClickListener() { 86  87       @Override 88       public void onClick(View v) { 89         showPopupMenu(iv); 90       } 91     }); 92     // 设置复用 93     TextView title = (TextView) findViewById(R.id.tv1); 94     title.setText("创建记事本"); 95  96     findViewById(R.id.iv2).setOnClickListener(new OnClickListener() { 97  98       @Override 99       public void onClick(View v) {100 101         save();102       }103     });104 105   }106 107   // 弹出保存与否对话框108   private void save() {109     final String title_first = title_text.getText().toString();110     final String text_first = text_text.getText().toString();111     AlertDialog dialog1 = new AlertDialog.Builder(activity).create();112     dialog1.setTitle("您还未保存,是否保存吗?");113     dialog1.setIcon(R.drawable.delete);114     dialog1.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {115 116       @SuppressWarnings("unchecked")117       @Override118       public void onClick(DialogInterface dialog, int which) {119 120         if (title_first.trim().equals("") || text_first.trim().equals("")) {121           effect = Effects.standard;122           NotifiActivity nba = new NotifiActivity(activity, "请输入标题和内容!", effect, R.id.notify_weizhi,123               R.drawable.dfdf);124           nba.show();125         } else {126           setDataToDB(title_first, text_first);127 128           activity.finish();129 130         }131 132       }133     });134 135     dialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {136 137       @Override138       public void onClick(DialogInterface dialog, int which) {139 140         activity.finish();141       }142     });143     dialog1.show();144   }145 146   // 获取手机返回按钮,添加判断事件147   @Override148   public boolean onKeyDown(int keyCode, KeyEvent event) {149     if (keyCode == KeyEvent.KEYCODE_BACK) {150       String title_first = title_text.getText().toString();151       String text_first = text_text.getText().toString();152       if (title_first.trim().equals("") || text_first.trim().equals("")) {153         effect = Effects.slideOnTop;154         NotifiActivity nba = new NotifiActivity(activity, "请输入标题和内容!", effect, R.id.notify_weizhi,155             R.drawable.dfdf);156         nba.show();157       } else {158         save();159       }160     }161 162     return false;163   }164 165   @Override166   protected void onResume() {167 168     // 获取设置的字体大小值169     SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);170     String s = sharedpreferences.getString("edit_text_key_1", "15");171     size = Float.parseFloat(s);172 173     title_text.setTextSize(size);174     text_text.setTextSize(size);175     super.onResume();176   }177 178   private Dao GetDao() {179     // 通过ORM数据库工具找到Dao工具类180     ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(this);181     dataDao = mDatabaseHelper.getUserDao();182     return dataDao;183   }184 185   public void setDataToDB(String title, String text) {186     GetDao();187 188     // 设置数据189 190     mdata.setTime_text(Untils.formatTimeInMillis(System.currentTimeMillis()));191     mdata.setTitle(title);192     mdata.setText(text);193 194     // 将数据保存到ORM数据库195     try {196       dataDao.createOrUpdate(mdata);197     } catch (SQLException e) {198       e.printStackTrace();199     }200   }201 202   private void showPopupMenu(View view) {203     // View当前PopupMenu显示的相对View的位置204     PopupMenu popupMenu = new PopupMenu(this, view);205 206     // menu布局207     popupMenu.getMenuInflater().inflate(R.menu.main1, popupMenu.getMenu());208 209     // menu的item点击事件210     popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {211       @Override212       public boolean onMenuItemClick(MenuItem item) {213         String title_first = title_text.getText().toString();214         String text_first = text_text.getText().toString();215 216         if (item.getItemId() == R.id.text_save) {217 218           if (title_first.trim().equals("") || text_first.trim().equals("")) {219             effect = Effects.standard;220             NotifiActivity nba = new NotifiActivity(activity, "请输入标题和内容!", effect, R.id.notify_weizhi,221                 R.drawable.dfdf);222             nba.show();223           } else {224             setDataToDB(title_first, text_first);225 226             activity.finish();227 228           }229 230         }231 232         return false;233       }234     });235 236     popupMenu.show();237   }238 239 }

第三方弹出框类:

 1 package com.lixu.jishiben; 2  3 import com.gitonway.lee.niftynotification.lib.Effects; 4 import com.gitonway.lee.niftynotification.lib.NiftyNotificationView; 5  6 import android.app.Activity; 7  8 public class NotifiActivity { 9 10   private Activity activity;11   private String msg;12   private Effects effect;13   private int id;14   private int image_id;15 16   public NotifiActivity(Activity activity, String msg, Effects effect, int id, int image_id) {17     this.activity = activity;18     this.msg = msg;19     this.effect = effect;20     this.id = id;21     this.image_id = image_id;22   }23 24   public void show() {25     NiftyNotificationView.build(activity, msg, effect, id).setIcon(image_id).show();26   }27 28 }

设置界面:

 1 package com.lixu.jishiben; 2  3 import com.lixu.jishiben.PreferenceActivity.SettingFragment; 4 import com.lixu.jishiben.untils.Untils; 5  6 import android.app.Activity; 7 import android.app.FragmentManager; 8 import android.app.FragmentTransaction; 9 import android.os.Bundle;10 import android.view.View;11 import android.view.Window;12 import android.widget.TextView;13 14 public class ActivitySetting extends Activity {15   @Override16   protected void onCreate(Bundle savedInstanceState) {17     super.onCreate(savedInstanceState);18 19     // 让程序全屏20     requestWindowFeature(Window.FEATURE_NO_TITLE);21     Untils.toggleFullscreen(this, true);22 23     setContentView(R.layout.setting);24 25     // 设置复用26     findViewById(R.id.iv2).setVisibility(View.GONE);27     // 设置复用28     findViewById(R.id.iv1).setVisibility(View.GONE);29     // 设置复用30     TextView title = (TextView) findViewById(R.id.tv1);31     title.setText("个性化设置");32 33     // 创建Fragment管理器34     FragmentManager fm = this.getFragmentManager();35     FragmentTransaction ft = fm.beginTransaction();36     // 将原有的新Activity覆盖到原有之上37     ft.replace(R.id.setting, new SettingFragment());38     // 提交39     ft.commit();40 41   }42 }


数据库类:

 1 package com.lixu.jishiben.database; 2  3 import com.j256.ormlite.field.DatabaseField; 4 import com.j256.ormlite.table.DatabaseTable; 5  6 @DatabaseTable(tableName = "datas") 7 public class Data { 8  9   public final static String USER_ID = "user_id";10   public final static String TITLE = "title";11   public final static String TEXT = "text";12   public final static String SYSTEM_TIME = "system_time";13 14   public Data() {15 16   }17 18   public Data(String text, String title, String time_text) {19     this.text = text;20     this.title = title;21     this.time_text = time_text;22   }23 24   @DatabaseField(generatedId = true, columnName = USER_ID)25   public int user_id;26 27   @DatabaseField(columnName = TITLE)28   public String title;29 30   @DatabaseField(columnName = TEXT)31   public String text;32 33   @DatabaseField(columnName = SYSTEM_TIME)34   public String time_text;35 36   public int getUser_id() {37     return user_id;38   }39 40   public void setUser_id(int user_id) {41     this.user_id = user_id;42   }43 44   public String getTitle() {45     return title;46   }47 48   public void setTitle(String title) {49     this.title = title;50   }51 52   public String getText() {53     return text;54   }55 56   public void setText(String text) {57     this.text = text;58   }59 60   public String getTime_text() {61     return time_text;62   }63 64   public void setTime_text(String string) {65     this.time_text = string;66   }67 68 }

 1 package com.lixu.jishiben.database; 2  3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6  7 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; 8 import com.j256.ormlite.dao.Dao; 9 import com.j256.ormlite.support.ConnectionSource;10 import com.j256.ormlite.table.TableUtils;11 12 public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {13 14   private static ORMLiteDatabaseHelper mDatabaseHelper = null;15   private Dao<Data, Integer> mUserDao = null;16 17   private final static String DataBase_NAME = "text.db";18   private final static int DataBase_VERSION = 1;19 20   public ORMLiteDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {21     super(context, DataBase_NAME, factory, DataBase_VERSION);22   }23 24   public static ORMLiteDatabaseHelper getInstance(Context context) {25     if (mDatabaseHelper == null) {26       mDatabaseHelper = new ORMLiteDatabaseHelper(context, DataBase_NAME, null, DataBase_VERSION);27     }28 29     return mDatabaseHelper;30   }31 32   @Override33   public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {34 35     try {36       TableUtils.createTableIfNotExists(connectionSource, Data.class);37     } catch (Exception e) {38       e.printStackTrace();39     }40   }41 42   @Override43   public void onUpgrade(SQLiteDatabase database, ConnectionSource arg1, int arg2, int arg3) {44 45   }46 47   /**48    * 每一个数据库中的表,要有一个获得Dao的方法。 可以使用一种更通用的模板方法如:49    * 50    * public Dao<Class, Integer> getORMLiteDao(Class cls) throws SQLException {51    * if (dao == null) { dao = getDao(cls); }52    * 53    * return dao; }54   */55   public Dao<Data, Integer> getUserDao() {56     if (mUserDao == null) {57       try {58         mUserDao = getDao(Data.class);59       } catch (java.sql.SQLException e) {60         e.printStackTrace();61       }62     }63 64     return mUserDao;65   }66 67   @Override68   public void close() {69     super.close();70     mUserDao = null;71   }72 }

图形绘制类:

 1 package com.lixu.jishiben.draws; 2  3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.util.AttributeSet; 8 import android.view.View; 9 10 public class Mydrawable extends View {11   private int posfirst = 0;12   // 圆球的数量13   private int count = 0;14 15   // 用这个构造方法,用其他会报错16   public Mydrawable(Context context, AttributeSet attrs) {17     super(context, attrs);18   }19 20   // 根据情况设置圆球数量21   public void setCount(int a) {22     count = a;23   }24 25   public void choose(int pos) {26     posfirst = pos;27     // 执行这个方法 会重新绘图28     this.invalidate();29   }30 31   @Override32   protected void onDraw(Canvas canvas) {33     super.onDraw(canvas);34 35     Paint p = new Paint();36     // 去掉图形的锯齿,使得圆球更圆滑37     p.setAntiAlias(true);38 39     // 获取屏幕X Y坐标40     float w = getWidth();41     float h = getHeight();42 43     // 圆球的间距(针对各种机型屏幕适配)44     int juli = (int) (w / 7);45 46     // 设置圆球的半径(针对各种机型屏幕适配)47     float radius = w / 50;48 49     for (int i = 0; i < count; i++) {50       if (i == posfirst) {51 52         p.setColor(Color.RED);53         // 画一个红色圆形54         canvas.drawCircle((w - (count - 1) * juli) / 2 + juli * i, (float) (h * 0.9), radius + w / 142, p);55       } else {56 57         p.setColor(Color.GRAY);58         // 画一个灰色圆形59         canvas.drawCircle((w - (count - 1) * juli) / 2 + juli * i, (float) (h * 0.9), radius, p);60 61       }62 63     }64 65   }66 67 }

设置FragmentActivity:

 1 package com.lixu.jishiben.PreferenceActivity; 2  3 import com.lixu.jishiben.R; 4 import android.content.SharedPreferences; 5 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 6 import android.os.Bundle; 7 import android.preference.EditTextPreference; 8 import android.preference.PreferenceFragment; 9 import android.preference.PreferenceManager;10 11 //安卓官方申明在3.0版本后最好不要用FragmentActivity 应该用PreferenceFragment,在从主Activity中调用。12 public class SettingFragment extends PreferenceFragment {13 14   private MyListener mMyListener = new MyListener();15   private EditTextPreference mEditTextPreference;16 17   @Override18   public void onCreate(Bundle savedInstanceState) {19     super.onCreate(savedInstanceState);20     // 读取值的通用方法21     SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());22 23     // 添加设置选项24     addPreferencesFromResource(R.25     // 找到各个控件按钮26     mEditTextPreference = (EditTextPreference) findPreference("edit_text_key_1");27     // 设置初始值28     String str2 = sharedpreferences.getString("edit_text_key_1", "null");29     mEditTextPreference.setSummary(str2);30 31     // 添加项目数据发生变化时候的监听32     sharedpreferences.unregisterOnSharedPreferenceChangeListener(mMyListener);33   }34 35   // 程序暂停时取消注册监听事件,使得代码更加完整。36   @Override37   public void onPause() {38     super.onPause();39     getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(mMyListener);40   }41 42   // 程序运行时注册43   @Override44   public void onResume() {45     super.onResume();46     getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(mMyListener);47   }48 49   private class MyListener implements OnSharedPreferenceChangeListener {50 51     @Override52     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {53 54       switch (key) {55       case "edit_text_key_1":56         String str1 = sharedPreferences.getString("edit_text_key_1", "null");57         mEditTextPreference.setSummary(str1);58 59         break;60 61       default:62         break;63       }64     }65   }66 67 }


工具类:

 1 package com.lixu.jishiben.untils; 2  3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 import android.app.Activity; 7 import android.view.WindowManager; 8  9 public class Untils {10   public static float size;11 12   // 获取系统时间的工具,将其格式化为可读的格式13   public static String formatTimeInMillis(long timeInMillis) {14 15     Calendar cal = Calendar.getInstance();16     cal.setTimeInMillis(timeInMillis);17     Date date = cal.getTime();18     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");19     String fmt = dateFormat.format(date);20 21     return fmt;22   }23 24   public static void toggleFullscreen(Activity activity, boolean fullScreen) {25     // fullScreen为true时全屏26 27     WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();28 29     if (fullScreen) {30       attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;31     } else {32       attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;33     }34 35     activity.getWindow().setAttributes(attrs);36   }37 }


界面布局类:

welcome.

 1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   tools:context="com.lixu.jishiben.WelcomeActivity" > 6  7   <com.lixu.jishiben.draws.Mydrawable 8     android:id="@+id/mydrawable" 9     android:layout_width="match_parent"10     android:layout_height="match_parent" />11 12   <android.support.v4.view.ViewPager13     android:id="@+id/vp"14     android:layout_width="match_parent"15     android:layout_height="match_parent" >16   </android.support.v4.view.ViewPager>17 18 </RelativeLayout>

welcome_first.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" > 6  7   <ImageView 8     android:id="@+id/iv_first" 9     android:layout_width="match_parent"10     android:layout_height="match_parent" />11 12 </LinearLayout>

welcome_end.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:id="@+id/LinearLayout1" 4   android:layout_width="match_parent" 5   android:layout_height="match_parent" 6   android:orientation="vertical" > 7  8   <ImageView 9     android:layout_weight="4"10     android:id="@+id/iv_end"11     android:layout_width="match_parent"12     android:layout_height="match_parent" />13 14   <Button15     android:layout_weight="1"16     android:id="@+id/btn"17     android:layout_width="wrap_content"18     android:layout_height="wrap_content"19     android:layout_gravity="center"20     android:text="进入记事本" />21 22 </LinearLayout>


title_demo.

 1 <??> 2 <RelativeLayout ="http://schemas.android.com/apk/res/android" 3   android:id="@+id/RelativeLayout1" 4   android:layout_width="match_parent" 5   android:layout_height="wrap_content" 6   android:background="@android:color/holo_blue_dark" > 7  8   <TextView 9     android:id="@+id/tv1"10     android:layout_width="wrap_content"11     android:layout_height="wrap_content"12     android:layout_alignParentTop="true"13     android:layout_centerHorizontal="true"14     android:gravity="top"15     android:text="木子记事本"16     android:textColor="@android:color/white"17     android:textSize="30sp" />18 19   <ImageView20     android:id="@+id/iv1"21     android:layout_width="40dp"22     android:layout_height="40dp"23     android:layout_centerHorizontal="true"24     android:layout_alignParentRight="true"25     android:layout_alignParentTop="true"26     android:src="@drawable/more" />27 28   <ImageView29     android:id="@+id/iv2"30     android:layout_width="40dp"31     android:layout_height="40dp"32     android:layout_alignParentLeft="true"33     android:layout_centerHorizontal="true"34     android:layout_alignParentTop="true"35     android:paddingLeft="2dp"36     android:src="@drawable/back" />37 38 </RelativeLayout>

setting.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" > 6  7   <include layout="@layout/title_demo" /> 8  9   <FrameLayout10     android:id="@+id/setting"11     android:layout_width="match_parent"12     android:layout_height="match_parent" >13   </FrameLayout>14 15 </LinearLayout>

notebook.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:background="@drawable/text" 6   android:orientation="vertical" > 7  8   <include layout="@layout/title_demo" /> 9 10   <LinearLayout11     android:id="@+id/notify_weizhi"12     android:layout_width="match_parent"13     android:layout_height="wrap_content"14     android:layout_weight="10"15     android:orientation="vertical" >16 17     <EditText18       android:id="@+id/title"19       android:layout_width="match_parent"20       android:layout_height="wrap_content"21       android:hint="请输入标题"22       android:textColor="@android:color/black" />23 24     <EditText25       android:id="@+id/text"26       android:layout_width="match_parent"27       android:layout_height="match_parent"28       android:layout_weight="8"29       android:gravity="top"30       android:hint="请填写内容"31       android:textColor="@android:color/black" />32   </LinearLayout>33 34 </LinearLayout>

main_activity.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:id="@+id/main" 4   android:layout_width="match_parent" 5   android:layout_height="match_parent" 6   android:background="@drawable/background" 7   android:orientation="vertical" > 8  9   <include layout="@layout/title_demo" />10 11   <ListView12     android:id="@+id/listview"13     android:layout_width="match_parent"14     android:layout_height="match_parent" />15 16 </LinearLayout>

listview_layout.

 1 <??> 2 <LinearLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" > 6  7   <TextView 8     android:id="@+id/biaoti" 9     android:layout_width="match_parent"10     android:layout_height="wrap_content"11     android:textColor="#00C853"12     android:textSize="25sp" />13 14   <TextView15     android:id="@+id/time"16     android:layout_width="match_parent"17     android:layout_height="wrap_content"18     android:textColor="@android:color/darker_gray"19     android:textSize="15sp" />20 21 </LinearLayout>

菜单

 1 <menu ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   tools:context="com.lixu.drawable.MainActivity" > 4  5   <item 6     android:id="@+id/new_text" 7     android:orderInCategory="100" 8     android:showAsAction="never" 9     android:title="创建一个记事本"/>10   <item11     android:id="@+id/jishiben_setting"12     android:orderInCategory="100"13     android:showAsAction="never"14     android:title="设置"/>15   <item16     android:id="@+id/exit"17     android:orderInCategory="100"18     android:showAsAction="never"19     android:title="退出"/>20 21 </menu>

 1 <menu ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   tools:context="com.lixu.drawable.MainActivity" > 4  5   <item 6     android:id="@+id/text_save" 7     android:orderInCategory="100" 8     android:showAsAction="never" 9     android:title="保存"/>10 11 </menu>

Preference.

 1 <??> 2 <PreferenceScreen ="http://schemas.android.com/apk/res/android" > 3  4   <PreferenceCategory android:title="记事本设置" > 5     <EditTextPreference 6       android:defaultValue="15" 7       android:icon="@drawable/ic_launcher" 8       android:key="edit_text_key_1" 9       android:summary="15"10       android:title="字体大小" />11 12     <Preference13       android:icon="@drawable/bokeyuan"14       android:summary="http://www.cnblogs.com/labixiaoxin/"15       android:title="机主博客" >16       <intent17         android:action="android.intent.action.VIEW"18         android:data="http://www.cnblogs.com/labixiaoxin/" />19     </Preference>20   </PreferenceCategory>21 22 </PreferenceScreen>

AndroidManifest:

 1 <??> 2 <manifest ="http://schemas.android.com/apk/res/android" 3   package="com.lixu.jishiben" 4   android:versionCode="1" 5   android:versionName="1.0" > 6  7   <uses-sdk 8     android:minSdkVersion="14" 9     android:targetSdkVersion="19" />10 11   <application12     android:allowBackup="true"13     android:icon="@drawable/welcome"14     android:label="@string/app_name"15     android:theme="@android:style/Theme.Holo.DialogWhenLarge.NoActionBar" >16     <activity17       android:name=".WelcomeActivity"18       android:label="@string/app_name" >19       <intent-filter>20         <action android:name="android.intent.action.MAIN" />21 22         <category android:name="android.intent.category.LAUNCHER" />23       </intent-filter>24     </activity>25     <activity android:name="com.lixu.jishiben.MainActivity" />26     <activity android:name="com.lixu.jishiben.NoteBookActivity" />27     <activity android:name="com.lixu.jishiben.ActivitySetting" />28   </application>29 30 </manifest>