values/styles.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="TextStyle"> <item name="android:textSize">18sp</item> <item name="android:layout_marginRight">10dp</item> <item name="android:gravity">right</item> </style> <style name="EditStyle"> <item name="android:layout_columnWeight">1</item> <item name="android:layout_gravity">fill_horizontal</item> <item name="android:textSize">18sp</item> <item name="android:inputType">number</item> <item name="android:layout_marginBottom">10dp</item> </style> <style name="ButtonStyle"> <item name="android:layout_width">200dp</item> <item name="android:textSize">18sp</item> <item name="android:textAllCaps">false</item> <item name="android:layout_gravity">center_horizontal</item> <item name="android:layout_margin">10dp</item> <item name="android:layout_columnSpan">2</item> </style> </resources> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@drawable/ic_add" /> </android.support.design.widget.CoordinatorLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <LinearLayout android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="20dp"></LinearLayout> </android.support.v4.widget.NestedScrollView> |
content_main2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:padding="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".Main2Activity" tools:showIn="@layout/activity_main2"> <TextView android:layout_columnSpan="2" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:text="วันสำคัญประจำปี" android:textSize="20dp" /> <TextView style="@style/TextStyle" android:text="วันที่ (1 - 31)" /> <EditText android:id="@+id/edit_date" style="@style/EditStyle" /> <TextView style="@style/TextStyle" android:text="เดือน (1 - 12)" /> <EditText android:id="@+id/edit_month" style="@style/EditStyle" /> <TextView style="@style/TextStyle" android:text="ชื่อวันสำคัญ" /> <EditText android:id="@+id/edit_day_name" style="@style/EditStyle" android:inputType="text" /> <Button android:id="@+id/button_add" style="@style/ButtonStyle" android:text="บันทึกข้อมูล" /> <Button android:id="@+id/button_clear" style="@style/ButtonStyle" android:text="Clear" /> <Button android:id="@+id/button_back" style="@style/ButtonStyle" android:text="ย้อนกลับ" /> </GridLayout> |
layout/item_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:columnCount="4"> <TextView android:id="@+id/text_date" android:layout_width="80dp" android:layout_gravity="center_vertical" android:text="วัน เดือน" android:textSize="18sp" /> <TextView android:id="@+id/text_day_name" android:layout_columnWeight="1" android:layout_gravity="center_vertical" android:text="ชื่อวันสำคัญ" android:textSize="18sp" /> <ImageButton android:id="@+id/button_update" android:layout_margin="5dp" android:src="@drawable/ic_edit" android:tint="@color/colorAccent" /> <ImageButton android:id="@+id/button_delete" android:layout_margin="5dp" android:src="@drawable/ic_delete" android:tint="@color/colorAccent" /> <!-- .เส้นแบ่งระหว่างรายการ --> <TextView android:layout_height="1dp" android:layout_columnSpan="4" android:layout_columnWeight="1" android:background="#999" /> </GridLayout> |
SQLiteHelper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.phaisarn.myapplication; import android.database.sqlite.SQLiteOpenHelper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; public class SQLiteHelper extends SQLiteOpenHelper { private static SQLiteHelper sqLiteDB; private static final String DB_NAME = "dbase"; private static final int DB_VERSION = 1; private SQLiteHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } public static synchronized SQLiteHelper getInstance(Context context) { if (sqLiteDB == null) { sqLiteDB = new SQLiteHelper(context.getApplicationContext()); } return sqLiteDB; } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS important_day (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "date INTEGER, " + "month INTEGER, " + "day_name TEXT)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private SQLiteHelper mSQLite; private SQLiteDatabase mDb; private String[] mThaiMonths = { "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค."}; /* {"มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"}; */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); */ startActivity(new Intent(MainActivity.this, Main2Activity.class)); } }); mSQLite = SQLiteHelper.getInstance(this); } @Override public void onStart() { super.onStart(); LayoutInflater inflater = getLayoutInflater(); LinearLayout root = (LinearLayout) findViewById(R.id.main_layout); root.removeAllViewsInLayout(); mDb = mSQLite.getReadableDatabase(); String sql = "SELECT * FROM important_day ORDER BY month, date"; final Cursor cursor = mDb.rawQuery(sql, null); int i = 0; while (cursor.moveToNext()) { View item = inflater.inflate(R.layout.item_layout, null); item.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); String str = ""; if (cursor.getInt(1) < 10) { str += "0"; } str += cursor.getString(1) + " " + mThaiMonths[cursor.getInt(2) - 1]; TextView textDate = (TextView) item.findViewById(R.id.text_date); textDate.setText(str); TextView textDayName = (TextView) item.findViewById(R.id.text_day_name); textDayName.setText(cursor.getString(3)); final ImageButton btUpdate = (ImageButton) item.findViewById(R.id.button_update); btUpdate.setTag(cursor.getInt(0)); btUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClickUpdate(btUpdate.getTag().toString()); } }); final ImageButton btDelete = (ImageButton) item.findViewById(R.id.button_delete); btDelete.setTag(cursor.getInt(0)); btDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClickDelete(Integer.valueOf(btDelete.getTag().toString())); } }); root.addView(item, i); i++; } cursor.close(); } private void onClickUpdate(String _id) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); intent.putExtra("_id", _id); startActivity(intent); } private void onClickDelete(final int _id) { new AlertDialog.Builder(MainActivity.this) .setIcon(R.mipmap.ic_launcher) .setTitle("ยืนยันการลบ") .setMessage("ท่านต้องการลบรายการนี้จริงหรือไม่?") .setPositiveButton("ตกลง", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String sql = "DELETE FROM important_day WHERE _id = " + _id; mDb = mSQLite.getWritableDatabase(); mDb.execSQL(sql); onStart(); } }) .setNegativeButton("ยกเลิก", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
Main2Activity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { private SQLiteHelper mSQLite; private SQLiteDatabase mDb; private EditText mEditDate; private EditText mEditMonth; private EditText mEditDayName; private int _id = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); mSQLite = SQLiteHelper.getInstance(this); mEditDate = (EditText) findViewById(R.id.edit_date); mEditMonth = (EditText) findViewById(R.id.edit_month); mEditDayName = (EditText) findViewById(R.id.edit_day_name); Button button = (Button) findViewById(R.id.button_add); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addImportantDay(); } }); Button buttonBack = (Button) findViewById(R.id.button_back); buttonBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); startActivity(new Intent(Main2Activity.this, MainActivity.class)); } }); Button buttonClear = (Button) findViewById(R.id.button_clear); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mEditDate.setText(""); mEditMonth.setText(""); mEditDayName.setText(""); } }); } @Override public void onStart() { super.onStart(); Intent intent = getIntent(); String id = intent.getStringExtra("_id"); //อย่าใช้ getIntExtra() if (id == null) { return; } else { _id = Integer.valueOf(id); } String sql = "SELECT * FROM important_day " + "WHERE _id = " + _id; mDb = mSQLite.getReadableDatabase(); Cursor cursor = mDb.rawQuery(sql, null); cursor.moveToNext(); mEditDate.setText(cursor.getString(1)); mEditMonth.setText(cursor.getString(2)); mEditDayName.setText(cursor.getString(3)); cursor.close(); } private void addImportantDay() { int date = Integer.valueOf(mEditDate.getText().toString()); int month = Integer.valueOf(mEditMonth.getText().toString()); String dayName = mEditDayName.getText().toString().trim(); if (!isDataComplete(date, month, dayName)) { return; } if (_id == -1) { insert(date, month, dayName); } else { update(date, month, dayName); } } private void insert(int date, int month, String dayName) { String sql = "INSERT INTO important_day(date, month, day_name) " + "VALUES(?, ?, ?)"; String[] args = {date + "", month + "", dayName}; mDb = mSQLite.getWritableDatabase(); mDb.execSQL(sql, args); showMessage("บันทึกข้อมูลแล้ว"); } private void update(int date, int month, String dayName) { String sql = "UPDATE important_day " + "SET date = ?, month = ?, day_name = ? " + "WHERE _id = ?"; String[] args = {date + "", month + "", dayName, _id + ""}; mDb = mSQLite.getWritableDatabase(); mDb.execSQL(sql, args); showMessage("บันทึกข้อมูลแล้ว"); } private boolean isDataComplete(int date, int month, String dayName) { if (date < 1 || date > 31) { showMessage("วันที่ต้องเป็นตัวเลข 1 - 31"); return false; } if (month < 1 || month > 12) { showMessage("เดือนต้องเป็นตัวเลข 1 - 12"); return false; } switch (month) { case 4: case 6: case 9: case 11: if (date == 31) { showMessage("เดือนที่กำหนดเพียง 30 วัน"); return false; } break; case 2: if (date > 29) { showMessage("เดือนที่กำหนดมี 28 - 29 วัน"); return false; } } if (dayName.equals("")) { showMessage("ยังไม่ได้กำหนดชื่อวันสำคัญ"); return false; } return true; } private void showMessage(String msg) { Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override public void onBackPressed() { finish(); } } |
รูปที่ใช้