編輯:關於android開發
一個較為完整的記事本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 @Override 111 public Fragment getItem(final int pos) { 112 113 Fragment f = new Fragment() { 114 @Override 115 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 @Override 124 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 @Override 152 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 // 設置復用xml裡面讓返回的指示箭頭消失 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 @Override 113 public boolean onMenuItemClick(MenuItem item) { 114 switch (item.getItemId()) { 115 case R.id.new_text: 116 Intent intent = new Intent(activity, NoteBookActivity.class); 117 // 故意設置一個值為-1 118 intent.putExtra(Data.USER_ID, -1); 119 startActivity(intent); 120 break; 121 122 case R.id.jishiben_setting: 123 // 這裡需要啟動一個中轉的Activity來啟動SettingFragment 124 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 @Override 148 protected void onPreExecute() { 149 super.onPreExecute(); 150 } 151 152 @Override 153 protected ArrayList<HashMap<String, Object>> doInBackground(String... params) { 154 155 return GetDataFromDB(); 156 157 } 158 159 @Override 160 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 @Override 178 public boolean onContextItemSelected(MenuItem item) { 179 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 180 // 獲取點擊哪條數據的下標 181 final int pos = info.position; 182 // 根據下標獲取到數據庫中對應數據的id 183 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 @Override 203 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 @Override 211 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 @Override 231 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 @Override 285 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 // 設置緩存list 305 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 @Override 340 public int getCount() { 341 return list.size(); 342 } 343 344 @Override 345 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 // 設置復用xml裡面的textview的內容 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 @Override 118 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 @Override 138 public void onClick(DialogInterface dialog, int which) { 139 140 activity.finish(); 141 } 142 }); 143 dialog1.show(); 144 } 145 146 // 獲取手機返回按鈕,添加判斷事件 147 @Override 148 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 @Override 166 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 @Override 212 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 @Override 16 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 // 設置復用xml裡面讓返回的指示箭頭消失 26 findViewById(R.id.iv2).setVisibility(View.GONE); 27 // 設置復用xml裡面讓三點消失 28 findViewById(R.id.iv1).setVisibility(View.GONE); 29 // 設置復用xml裡面的textview的內容 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 @Override 33 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 @Override 43 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 @Override 68 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 @Override 32 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 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 // 讀取值的通用方法 21 SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 22 23 // 添加設置選項 24 addPreferencesFromResource(R.xml.preference); 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 @Override 37 public void onPause() { 38 super.onPause(); 39 getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(mMyListener); 40 } 41 42 // 程序運行時注冊 43 @Override 44 public void onResume() { 45 super.onResume(); 46 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(mMyListener); 47 } 48 49 private class MyListener implements OnSharedPreferenceChangeListener { 50 51 @Override 52 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.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="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.ViewPager 13 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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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 <Button 15 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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="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 <ImageView 20 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 <ImageView 29 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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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 <FrameLayout 10 android:id="@+id/setting" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" > 13 </FrameLayout> 14 15 </LinearLayout>
notebook.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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 <LinearLayout 11 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 <EditText 18 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 <EditText 25 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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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 <ListView 12 android:id="@+id/listview" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" /> 15 16 </LinearLayout>
listview_layout.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="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 <TextView 15 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>
菜單xml:
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="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 <item 11 android:id="@+id/jishiben_setting" 12 android:orderInCategory="100" 13 android:showAsAction="never" 14 android:title="設置"/> 15 <item 16 android:id="@+id/exit" 17 android:orderInCategory="100" 18 android:showAsAction="never" 19 android:title="退出"/> 20 21 </menu>
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="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.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <PreferenceScreen xmlns:android="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 <Preference 13 android:icon="@drawable/bokeyuan" 14 android:summary="http://www.cnblogs.com/labixiaoxin/" 15 android:title="機主博客" > 16 <intent 17 android:action="android.intent.action.VIEW" 18 android:data="http://www.cnblogs.com/labixiaoxin/" /> 19 </Preference> 20 </PreferenceCategory> 21 22 </PreferenceScreen>
AndroidManifest:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="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 <application 12 android:allowBackup="true" 13 android:icon="@drawable/welcome" 14 android:label="@string/app_name" 15 android:theme="@android:style/Theme.Holo.DialogWhenLarge.NoActionBar" > 16 <activity 17 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>
Android應用一般上架流程 Android App上架所需文件 上架准備 App文件: 1.安裝包 2.應用商標:圖片格式PNG,大小512*512,小於800KB。
Android JDK配置使支持Gradle更新,Maven安裝,gradlemaven配置Maven 或執行Gradle更新等相關命令時出現以下錯誤時要重新配置JDK
android:Activity數據傳遞之全局對象(application) 在activity之間數據傳遞中還有一種比較實用的方式,就是全局對象applicatio
教新手一步步解決:Plugin is too old, please update to a more recent version, or set ANDROID_DA