便签添加和修改活动基类

职责

  • 为添加便签提供note对象
  • 为修改便签提供note对象

组件

  • protected EditText info; 文本框
  • protected Toolbar toolbar; 工具栏
  • protected Note note; Note对象

事件

  • 点击手机导航栏的返回按键,使用重写public void finish()方法处理返回事件,因为需要提供返回编码,所以只能在public void finish()之前处理,不可以在protected void onDestroy()之前处理

抽象方法

  • public abstract void init();//初始化方法
  • public abstract void doing();//业务方法

res/layout/activity_base.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.hzj163.mysqlitedb.BaseActivity">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        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"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_base"/>
</android.support.design.widget.CoordinatorLayout>

res/layout/content_base.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_add"
    tools:context="com.hzj163.mysqlitedb.BaseActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        >
        <EditText
            android:id="@+id/info"
            android:gravity="left|top"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </ScrollView>

</LinearLayout>

com.hzj163.mysqlitedb.BaseActivity.java


package com.hzj163.mysqlitedb;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.DateFormat;
import android.widget.EditText;

import com.hzj163.mysqlitedb.beans.Note;

import java.util.Date;


//便器为单页面操作,所以不需要返回父亲Activity,也不需要任何图标
//BaseActivity共开出两个方法一个是init,一个是doing,主要是添加和修改不一样
public abstract class BaseActivity extends AppCompatActivity {
    //便签内容文本框
    protected  EditText info;
    //工具栏对象
    protected Toolbar toolbar;
    //便签对象
    protected Note note;
    //活动开始的时候调用onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_base);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        info = (EditText) findViewById(R.id.info);
        //初始化【具体细节由子类设置】
        init();
        setSupportActionBar(toolbar);

    }

    //初始化方法
    public abstract void init();
    //业务方法
    public abstract void doing();

    //界面返回的时候处理任务
    @Override
    public void finish() {
        //业务统一在活动退出的时候调度【具体细节由子类实现】
        doing();
        super.finish();
    }
}