Android实战:利用SQLite实现简单的记事本(笔记本)

新建工程(Android Studio)

略。

添加、编辑资源文件

  1. 编辑res->values->strings.xml文件,内容如下:
<resources>
    <string name="app_name">MyNoteBook</string>
    <string name="title">记事本</string>
    <string name="btn_confirm">确定</string>
    <string name="btn_cancel">取消</string>
    <string name="btn_add">添加</string>
    <string name="btn_save">保存</string>
    <string name="btn_return">返回</string>
    <string name="save_succ">保存成功!</string>
    <string name="save_fail">保存失败!标题或内容不能为空!</string>
</resources>
  1. res->drawable目录下添加shape.xml文件用于实现按钮的圆角效果,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#1C82C4"/>
    <!-- android:radius 按钮四角弧形的半径 -->
    <corners android:radius="8dip" />
    <!-- padding:Button里面的文字与Button边界的内边距 -->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
</shape>
  1. 编辑res->values->style.xml文件,在其中定义全屏风格,美化界面。内容如下:
<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="FullTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>
  1. AndroidManifest.xml文件中应用全屏风格。更改application元素android:theme属性即可。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zys.mynotebook">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/FullTheme">
        <activity android:name=".EditActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
  1. 更改程序图标。
    略。

新建一个Activity,EditActivity作为笔记编辑、浏览界面

先保留自动生成的布局文件activity_edit.xml文件。

编辑、添加布局文件

  1. 编辑res->layout路径下的主界面布局文件activity_main.xml,主要利用ListView来显示所有笔记的标题和事件,内容如下:
<?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:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#03AEDA"
        android:orientation="horizontal">
        <!-- 图标-->
        <ImageView
            android:src="@drawable/note_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="2dp"/>
        <TextView
            android:id="@+id/music_list_title"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginBottom="6dp"
            android:text="@string/title"
            android:textSize="16dp"
            android:textColor="#FFFFFF"
            android:paddingLeft="10dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="3dp"
            android:gravity="center_vertical" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="2dp">
            <Button
                android:id="@+id/btn_add"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:text="@string/btn_add"
                android:background="@drawable/shape"
                android:textColor="#B9E3FD"
                android:layout_gravity="right"
                android:layout_marginRight="8dp"
                android:layout_marginTop="2dp" />
        </LinearLayout>
    </LinearLayout>
    <ListView
        android:id="@+id/note_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>
  1. 添加在res->layout目录下添加布局文件item_layout.xml文件,作为主界面中ListView的每个item的布局,内容如下:
<?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="70dp"
    android:padding="5dp"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/rand_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="2dp"
        android:layout_margin="4dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height=
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值