Android實(shí)現(xiàn)記事本功能(26)
本文實(shí)例為大家分享了Android實(shí)現(xiàn)記事本功能的具體代碼,供大家參考,具體內(nèi)容如下
MainActivity.java代碼:
package siso.smartnotef.activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import siso.smartnotef.R;
import siso.smartnotef.adapter.NotepadeAdapter;
import siso.smartnotef.db.DataHelper;
import siso.smartnotef.global.GlobalParams;
import siso.smartnotef.model.NotepadBean;
import siso.smartnotef.model.NotepadWithDataBean;
import siso.smartnotef.service.MainService;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, NotepadeAdapter.ClickFunction {
private TextView tv_add;
private ListView lv_contents;
private List<NotepadWithDataBean> notepadWithDataBeanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1 = new Intent(MainActivity.this, MainService.class);
startService(intent1);
findViews();
setListeners();
initView();
}
private void findViews() {
tv_add = (TextView) findViewById(R.id.tv_add);
lv_contents = (ListView) findViewById(R.id.lv_content);
}
private void setListeners() {
tv_add.setOnClickListener(this);
}
private void initView() {
DataHelper helper = new DataHelper(MainActivity.this);
notepadWithDataBeanList = new ArrayList<NotepadWithDataBean>();
List<NotepadBean> notepadBeanList = helper.getNotepadList();
for (int i = 0; i < notepadBeanList.size(); i++) {
if (0 == notepadWithDataBeanList.size()) {
NotepadWithDataBean notepadWithDataBean = new NotepadWithDataBean();
notepadWithDataBean.setData(notepadBeanList.get(0).getDate());
notepadWithDataBeanList.add(notepadWithDataBean);
}
boolean flag = true;
for (int j = 0; j < notepadWithDataBeanList.size(); j++) {
int date = notepadWithDataBeanList.get(j).getData();
if (date == notepadBeanList.get(i).getDate()) {
notepadWithDataBeanList.get(j).getNotepadBeenList().add(notepadBeanList.get(i));
flag = false;
break;
}
}
if (flag) {
NotepadWithDataBean notepadWithDataBean = new NotepadWithDataBean();
notepadWithDataBean.setData(notepadBeanList.get(i).getDate());
notepadWithDataBeanList.add(notepadWithDataBean);
notepadWithDataBeanList.get(notepadWithDataBeanList.size() - 1).getNotepadBeenList().add(notepadBeanList.get(i));
}
}
NotepadeAdapter adapter = new NotepadeAdapter(MainActivity.this, notepadWithDataBeanList, this);
lv_contents.setAdapter(adapter);
// setListViewHeightBasedOnChildren(lv_contents);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
if (listView == null) return;
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_add:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt(GlobalParams.TYPE_KEY, GlobalParams.TYPE_ADD);
intent.putExtras(bundle);
intent.setClass(MainActivity.this, AddContentActivity.class);
startActivityForResult(intent, GlobalParams.ADD_REQUEST);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GlobalParams.ADD_REQUEST:
if (GlobalParams.ADD_RESULT_OK == resultCode) {
initView();
}
break;
}
}
@Override
public void clickItem(int position, int itemPosition) {
Bundle bundle = new Bundle();
bundle.putInt(GlobalParams.TYPE_KEY, GlobalParams.TYPE_EDIT);
bundle.putSerializable(GlobalParams.BEAN_KEY, notepadWithDataBeanList.get(position));
bundle.putInt(GlobalParams.ITEM_POSITION_KEY, itemPosition);
Intent intent = new Intent(this, AddContentActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent, GlobalParams.ADD_REQUEST);
}
@Override
public void longClickItem(final int position, final int itemPostion) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("確認(rèn)刪除嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DataHelper helper = new DataHelper(MainActivity.this);
helper.deleteNotepad(notepadWithDataBeanList.get(position).getNotepadBeenList().get(itemPostion).getId());
initView();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
AddContentActivity.java代碼:
package siso.smartnotef.activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
import siso.smartnotef.R;
import siso.smartnotef.db.DataHelper;
import siso.smartnotef.global.GlobalParams;
import siso.smartnotef.model.NotepadBean;
import siso.smartnotef.model.NotepadWithDataBean;
public class AddContentActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_save;
private TextView tv_date;
private TextView tv_time;
private TextView tv_cancel;
private EditText et_content;
private String time = "";
private String date = "";
private Bundle bundle;
private int type;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_content);
bundle=getIntent().getExtras();
type=bundle.getInt(GlobalParams.TYPE_KEY);
findViews();
setListeners();
initDate();
}
private void findViews() {
et_content=(EditText)findViewById(R.id.et_content);
tv_save = (TextView) findViewById(R.id.tv_save);
tv_date = (TextView) findViewById(R.id.tv_date);
tv_time = (TextView) findViewById(R.id.tv_time);
tv_cancel=(TextView)findViewById(R.id.tv_cancel);
}
private void setListeners() {
tv_save.setOnClickListener(this);
tv_date.setOnClickListener(this);
tv_time.setOnClickListener(this);
tv_cancel.setOnClickListener(this);
}
private void initDate() {
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
date=getDate(year,month,day);
if(type==GlobalParams.TYPE_EDIT){
NotepadWithDataBean notepadWithDataBean=(NotepadWithDataBean)(bundle.getSerializable(GlobalParams.BEAN_KEY));
et_content.setText(notepadWithDataBean.getNotepadBeenList().get(bundle.getInt(GlobalParams.ITEM_POSITION_KEY)).getContent());
date=notepadWithDataBean.getData()+"";
tv_date.setText(date);
time=notepadWithDataBean.getNotepadBeenList().get(bundle.getInt(GlobalParams.ITEM_POSITION_KEY)).getTime();
tv_time.setText(time);
}
}
private String getDate(int year,int month,int day){
String date="";
date+=year;
if(month<9){
date=date+"0"+(month+1);
}else{
date+=(month+1);
}
if(day<10){
date=date+"0"+day;
}else {
date+=day;
}
return date;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_save:
if(type==GlobalParams.TYPE_EDIT){
update();
}else {
save();
}
break;
case R.id.tv_date:
selectDateDialog();
break;
case R.id.tv_time:
selectTimeDialog();
break;
case R.id.tv_cancel:
finish();
break;
}
}
private void selectDateDialog(){
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
final int month=c.get(Calendar.MONTH)+1;
int day=c.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(AddContentActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date=getDate(year,monthOfYear,dayOfMonth);
tv_date.setText(date);
}
},year,month,day).show();
}
private void selectTimeDialog() {
Calendar c = Calendar.getInstance();
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
new TimePickerDialog(AddContentActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
time=formatTime(hourOfDay,minute);
tv_time.setText(time);
}
}, mHour, mMinute, true).show();
}
private String formatTime(int hour,int minute){
String time=hour+":";
if(minute<10){
time=time+"0"+minute;
}else{
time+=minute;
}
return time;
}
private void save() {
String content = et_content.getText().toString();
if ("".equals(content)) {
Toast.makeText(AddContentActivity.this, "請(qǐng)輸入內(nèi)容", Toast.LENGTH_SHORT).show();
return;
}
if ("".equals(time)) {
Toast.makeText(AddContentActivity.this, "請(qǐng)選擇時(shí)間", Toast.LENGTH_SHORT).show();
return;
}
NotepadBean notepadBean = new NotepadBean();
notepadBean.setContent(content);
notepadBean.setDate(Integer.parseInt(date));
notepadBean.setTime(time);
DataHelper helper = new DataHelper(AddContentActivity.this);
helper.insertData(notepadBean);
setResult(GlobalParams.ADD_RESULT_OK);
finish();
}
private void update(){
DataHelper helper=new DataHelper(AddContentActivity.this);
NotepadWithDataBean bean=(NotepadWithDataBean)(bundle.getSerializable(GlobalParams.BEAN_KEY));
int itemPosition=bundle.getInt(GlobalParams.ITEM_POSITION_KEY);
helper.update(bean.getNotepadBeenList().get(itemPosition).getId(),date,time,et_content.getText().toString());
setResult(GlobalParams.ADD_RESULT_OK);
finish();
}
}
RemindActivity.java代碼:
package siso.smartnotef.activity;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import siso.smartnotef.R;
import siso.smartnotef.global.GlobalParams;
public class RemindActivity extends AppCompatActivity {
private TextView tv_content;
private Button bt_confirm;
private MediaPlayer mMediaPlayer;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remind);
findViews();
setListeners();
Bundle bundle=getIntent().getExtras();
String content=bundle.getString(GlobalParams.CONTENT_KEY);
tv_content.setText(content);
playSound();
}
private void findViews(){
tv_content=(TextView)findViewById(R.id.tv_content);
bt_confirm=(Button) findViewById(R.id.bt_confirm);
}
private void setListeners(){
bt_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null!=mMediaPlayer){
mMediaPlayer.stop();
finish();
}
}
});
}
public void playSound() {
new Thread(new Runnable() {
@Override
public void run() {
mMediaPlayer = MediaPlayer.create(RemindActivity.this, getSystemDefultRingtoneUri());
mMediaPlayer.setLooping(true);//設(shè)置循環(huán)
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
}
}).start();
}
//獲取系統(tǒng)默認(rèn)鈴聲的Uri
private Uri getSystemDefultRingtoneUri() {
return RingtoneManager.getActualDefaultRingtoneUri(RemindActivity.this,
RingtoneManager.TYPE_RINGTONE);
}
}
activity_main.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="siso.smartnotef.activity.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/title_color" android:paddingLeft="10dp" android:paddingRight="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="18sp" android:layout_centerInParent="true" android:text="智能記事本"/> <TextView android:id="@+id/tv_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:text="新增" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:textSize="13sp"/> </RelativeLayout> <ListView android:id="@+id/lv_content" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
activity_add_content.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_add_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="siso.smartnotef.activity.AddContentActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/title_color" android:paddingLeft="10dp" android:paddingRight="10dp"> <TextView android:id="@+id/tv_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="取消" android:textColor="@color/white"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="18sp" android:layout_centerInParent="true" android:text="智能記事本"/> <TextView android:id="@+id/tv_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:text="保存" android:layout_centerVertical="true" android:layout_alignParentRight="true" /> </RelativeLayout> <LinearLayout android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="今天"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" -- "/> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="請(qǐng)選擇時(shí)間" /> </LinearLayout> <EditText android:id="@+id/et_content" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textMultiLine" android:gravity="left|top" android:layout_margin="20dp" android:padding="10dp" android:hint="請(qǐng)輸入內(nèi)容" android:background="@drawable/edit_back"/> </LinearLayout>
activity_remind.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" tools:context="siso.smartnotef.activity.RemindActivity"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/bt_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="取消" /> </LinearLayout>
AndroidManifest.xml內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="siso.smartnotef"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".activity.MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".receiver.MainReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <activity android:name=".activity.AddContentActivity" /> <service android:name=".service.MainService" android:enabled="true" android:exported="true" /> <activity android:name=".activity.RemindActivity" ></activity> </application> </manifest>
項(xiàng)目結(jié)構(gòu)如圖:

項(xiàng)目運(yùn)行結(jié)果如圖:




以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- Android實(shí)現(xiàn)記事本小功能
- Android記事本項(xiàng)目開發(fā)
- Android實(shí)現(xiàn)記事本功能
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- android實(shí)現(xiàn)記事本app
- Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能實(shí)例
- Android中實(shí)現(xiàn)記事本動(dòng)態(tài)添加行效果
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
- Android手機(jī)開發(fā)設(shè)計(jì)之記事本功能
相關(guān)文章
Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解
這篇文章主要介紹了Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解的相關(guān)資料,這里說明實(shí)現(xiàn)的思路及實(shí)現(xiàn)方法,需要的朋友可以參考下2017-07-07
Android封裝高德地圖定位工具類Util的詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于Android封裝高德地圖定位工具類Util的相關(guān)資料,封裝成工具類后非常方便以后的項(xiàng)目,可以直接使用,文中也給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2021-07-07
Android自定義View制作動(dòng)態(tài)炫酷按鈕實(shí)例解析
這篇文章主要為大家詳細(xì)解析了Android自定義View制作動(dòng)態(tài)炫酷按鈕實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
利用Kotlin Tools如何快速添加Kotlin依賴詳解
這篇文章主要給大家介紹了關(guān)于利用Kotlin Tools如何快速添加Kotlin依賴的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android中pendingIntent與Intent的深入分析
這篇文章主要介紹了Android中pendingIntent的深入分析的相關(guān)資料,需要的朋友可以參考下2017-04-04
React?Native之在Android上添加陰影的實(shí)現(xiàn)
這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Flutter使用SingleTickerProviderStateMixin報(bào)錯(cuò)解決
這篇文章主要為大家介紹了Flutter使用SingleTickerProviderStateMixin報(bào)錯(cuò)解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
如何利用adb卸載手機(jī)預(yù)裝軟件(系統(tǒng)軟件)
對(duì)于Android手機(jī)通常有很多不必要的預(yù)置軟件,但是又無法卸載,占用桌面有很難受,所以本次使用adb工具來實(shí)現(xiàn)從電腦命令來卸載或停用軟件,下面這篇文章主要給大家介紹了關(guān)于如何利用adb卸載手機(jī)預(yù)裝軟件(系統(tǒng)軟件)的相關(guān)資料,需要的朋友可以參考下2022-09-09

