前言
因为暑假回家,家里wifi坏了,想着自学安卓这么久了,连一个自己的哪怕很简陋的app都没写过,所以就花了点时间做一下,记得当初才开始学编程的时候,学习群里有哥们发了自己写的记事本软件,所以这次我也做个记事本 app 吧 (很随便).
准备知识
- Android Studio (v2.3)
- ListView
- SQLite
- 文件存储
- 自定义 view
开始动手吧
主要使用点击事件,创建 Intent 实例 , 实现 activity 之间的切换 . 整体上并不难,其中自定义了 Toast , 在 Toast 提示窗口加入了图片显示.在网上找了一下自定义 Toast 的blog , 一大堆代码 , 挺麻烦的,于是自己看了一下 Toast 的文档,发现有一个 setView() 方法:

我的理解就是自己做一个 layout 然后传到这个方法里面 , 就可以达到你自己想要的效果
public class ToastView{
private static TextView toastText;
private static ImageView toastImageView;
public static void showToast(Context context, String message, Drawable image){
Toast toast = new Toast(context);
View toastView = LayoutInflater.from(context).inflate(R.layout.toast_view,null);
toastText= (TextView) toastView.findViewById(R.id.toast_text);
toastText.setText(message);
toastText.setTextSize(26);
toastImageView= (ImageView) toastView.findViewById(R.id.toast_image_view);
if(image!=null){
toastImageView.setImageDrawable(image);
}
toast.setView(toastView);
toast.show();
}
public static void showToast2(Context context, String message, int image){
Toast toast = new Toast(context);
View toastView = LayoutInflater.from(context).inflate(R.layout.toast_view,null);
toastText= (TextView) toastView.findViewById(R.id.toast_text);
toastText.setText(message);
toastText.setTextSize(26);
toastImageView= (ImageView) toastView.findViewById(R.id.toast_image_view);
toastImageView.setImageResource(image);
toast.setView(toastView);
toast.show();
}
}
有两个方法都可以进行自定义 view 的实现,主要是传入的图片参数不同, showToast() 是传入一个 Drawable 对象, showToast2() 是传入图片资源的 id.
下面是自定义 Toast 的布局:
<!-- toast_view.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:background="@drawable/toast_background"
android:orientation="horizontal">
<ImageView
android:id="@+id/toast_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:src="@mipmap/ic_touxiang" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
在每次编辑完成后,保存时会产生一个 uuid 作为编辑内容存储到内存的文件名,同时将 uuid 保存到数据库 , 便于创建笔记列表的时候, listview 通过获取数据库里面的 uuid 读取对应的文件,删除 listview 的 item 就删除数据库中对应的 uuid 和 uuid 对应的文件 (这些都很简单,就不赘述了)
但是在删除 item 的同时删除数据库中的 uuid 这里卡了一下,无奈之下,只好在每个自定义的 item 布局中加入一个 TextView 将这个 TextView 的内容设为每个 item 对应文件的 uuid .
<!-- listview_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/list_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="@+id/imageView2"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/un_visiable"
android:visibility="gone"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:textSize="15sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:maxLines="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
android:layout_marginBottom="0dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.21614583" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
android:layout_marginRight="8dp" />
</android.support.constraint.ConstraintLayout>
最后加入了一个 FloatingActionButton ,没啥用,纯粹为了装逼,而且 MainActivity 非常蛋疼的搞了个侧滑菜单,也没用上.
github地址 : https://github.com/killerYe/MyApp
新手上道,没有写博客的经验,也没开发经验,哪里做得不好,指点指点就好,别喷哦 ! ! !
因为我也是刚学 android 不久,所以有兴趣的同学可以加个微信, 一起学习, 讨论一下哦 ! ! !
