Android 便签开发源码
首先我们来看一下app截图:
因为代码比较多,这里我把主要的代码贴出来,
源码链接:https://download.csdn.net/download/qq_40748996/11202859
GitHub:https://github.com/bbcode1/Notepad
程序结构图
activity_main.xml:
<?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"
android:fitsSystemWindows="true"
tools:context="com.example.notes_2.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:id="@+id/aaa">
<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>
<ListView
android:layout_below="@+id/aaa"
android:id="@+id/listview_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<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:src="@android:drawable/ic_menu_add"
android:layout_marginLeft="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:layout_alignBottom="@+id/listview_title"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
>
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成"
android:background="@color/colorPrimary"
/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:format12Hour="yyyy年dd月MM日 HH:mm"
/>
<Button
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:background="@color/colorPrimary"
/>
</LinearLayout>
<EditText
android:id="@+id/et_content"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:hint="Type something here"
/>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.notes_2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:id="@+id/time" />
<TextView
android:id="@+id/content"
android:layout_below="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="15sp"
android:paddingBottom="15sp" />
</RelativeLayout>
modify.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:background="@color/colorPrimary"
/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:format12Hour="yyyy年dd月MM日 HH:mm"
/>
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:background="@color/colorPrimary"
/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"
android:background="@color/colorPrimary"
/>
</LinearLayout>
<EditText
android:id="@+id/et_modify"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Content.java
package com.example.notes_2;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Content extends Activity implements View.OnClickListener {
private Button okButton,cancleButton;
private EditText contentWrite;
private DBHelper dbHelper;
private Cursor cursor;
private int _id=0;
private Time time;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
String string=formatter.format(curDate);
Log.e("0",string);
init();
}
private void init(){
dbHelper=new DBHelper(this);
cursor=dbHelper.select();
okButton = (Button) findViewById(R.id.btn_ok);
cancleButton = (Button) findViewById(R.id.btn_cancle);
contentWrite = (EditText) findViewById(R.id.et_content);
okButton.setOnClickListener(this);
cancleButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_ok:
addData() ;
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
break;
case R.id.btn_cancle:
Intent intent1 = new Intent(this,MainActivity.class);
startActivity(intent1);
finish();
break;
}
}
private void addData(){
if (contentWrite.getText().toString().equals("")){
Toast.makeText(Content.this,"内容不能为空",Toast.LENGTH_SHORT).show();
}else{
dbHelper.insert(contentWrite.getText().toString());
cursor.requery();
contentWrite.setText("");
_id=0;
}
}
}
DBHelper.java
package com.example.notes_2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private final static String DB_NAME="my.db";
private final static int DB_VERSION=1;
private final static String TABLE_NAME="info";
private final static String CONTENT="content";
private final static String ID="_id";
SQLiteDatabase database=getWritableDatabase();
public DBHelper(Context context){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database){
database.execSQL("create table " + TABLE_NAME + "(" + ID + " integer primary key autoincrement," + CONTENT + " text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(String text){
ContentValues contentValues=new ContentValues();
contentValues.put("content",text);
long row=database.insert(TABLE_NAME,null,contentValues);
return row;
}
public void update(int _id,String text){
ContentValues contentValues=new ContentValues();
contentValues.put("content",text);
database.update(TABLE_NAME,contentValues,ID+"=?",new String[]{Integer.toString(_id)});
}
public void delete(int _id){
database.delete(TABLE_NAME, ID + "=?", new String[]{Integer.toString(_id)});
}
public Cursor select(){
Cursor cursor=database.query(TABLE_NAME,null,null,null,null,null,null);
return cursor;
}
}
MainActivity.java
package com.example.notes_2;
import android.content.Intent;
import android.database.Cursor;
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.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private Cursor cursor;
private DBHelper dbHelper;
private int _id=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DBHelper(this);
cursor=dbHelper.select();
listView=(ListView)findViewById (R.id.listview_title);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.list_item,cursor, new String[]{"content"},new int[]{R.id.content});
listView.setAdapter(adapter);
listView.invalidateViews();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
_id=cursor.getInt(0);
Intent intent=new Intent(MainActivity.this,Modify.class);
intent.putExtra("id",_id);
intent.putExtra("data",cursor.getString(1));//getString(1)显示cursor该列的内容
startActivity(intent);
finish();
}
});
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) {
Intent intent = new Intent(MainActivity.this,Content.class);
startActivity(intent);
}
});
}
@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);
}
}
Modify.java
package com.example.notes_2;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Modify extends Activity implements View.OnClickListener {
private EditText et_show;
private Button updateButton, deleteButton, backButton;
private DBHelper dbHelper;
private Cursor cursor;
private int id;
@Override
protected void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.modify);
init();
}
private void init() {
et_show = (EditText) findViewById(R.id.et_modify);
Intent intent = getIntent();
String data = intent.getStringExtra("data");
id = intent.getIntExtra("id", id);
et_show.setText(data);
dbHelper = new DBHelper(this);
cursor = dbHelper.select();
updateButton = (Button) findViewById(R.id.btn_update);
deleteButton = (Button) findViewById(R.id.btn_delete);
backButton = (Button) findViewById(R.id.btn_back);
updateButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
backButton.setOnClickListener(this);
}
public void updateData() {
if (id == 0) {
Toast.makeText(Modify.this, "内容不能为空", Toast.LENGTH_SHORT).show();
} else {
dbHelper.update(id, et_show.getText().toString());
cursor.requery();
id = 0;
}
}
public void deleteData(){
if (id==0){
Toast.makeText(Modify.this, "内容不能为空", Toast.LENGTH_SHORT).show();
}else{
dbHelper.delete(id);
cursor.requery();
id=0;
}
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn_update:
updateData();
Intent intent1 = new Intent(this,MainActivity.class);
startActivity(intent1);
finish();
break;
case R.id.btn_delete:
deleteData();
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
break;
case R.id.btn_back:
Intent intent2 = new Intent(this,MainActivity.class);
startActivity(intent2);
finish();
break;
}
}
}