1. 程式人生 > >《第一行程式碼》第二版 學習總結15 資料持久化之LitePal開源庫的基本使用

《第一行程式碼》第二版 學習總結15 資料持久化之LitePal開源庫的基本使用

      最近利用下班時間,找了看什麼書比較適合初學android的朋友,很多人推薦了這本書,於是就買了一本,感覺看書,思考,動手,再思考和總結這樣過程還是很有必要的,於是就打算把自己學習的東西簡單的總結一下;方便自己以後查詢,也有利於學習的鞏固。在這裡首先要感謝一下書籍的作者——郭霖前輩。

      關於資料持久化我在前面三篇已經介紹三種實現方式(操作檔案SPSQLite資料庫實現);今天就來介紹一下LitePal開源庫的基本使用,其實在書的前面關於網路那一部分就介紹了一個開源庫---OkHttp的使用,只是那一部分我的Demo寫了,現在還沒有放在部落格上,今天剛好也介紹一下開源庫的整個使用流程。

1,環境與配置準備

第一步:開源庫Jar包的引入

(1)第一種:在app/build.gradle檔案的dependencies下新增:

compile 'org.litepal.android:core:LitePal版本號';其中版本號到開源專案主頁查詢(比如1.4.1)

(2)第二種:直接下載開源專案,解壓找到對應jar包,新增在工程libs目錄下,右鍵選擇Add as library也行

第二步:在app/src/main目錄下,建立一個assets目錄,在裡面新增litepal.xml檔案,主要用於資料庫配置,具體內容包括資料庫名稱,版本以及OR對映(物件/關係)

第三步:在主配置檔案中的application的屬性中新增一條:

android:name="org,litepal.LitePalApplication"

2,示例程式碼

下面的示例程式碼較多,並且在並沒有完善,尤其在資料更新沒有做二級頁面,所以有興趣的朋友可以繼續完善。

MainActivity.java程式碼:

package com.hfut.litepaldemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import org.litepal.LitePal;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createDB(View view){
        LitePal.getDatabase();
        Log.i(TAG, "createDB: 執行了建立資料庫操作");
    }
    public void updateDB(View view){
        LitePal.getDatabase();
        Log.i(TAG, "updateDB: 執行了更新資料庫操作");
    }

    public void operateDB(View view){
        Intent intent=new Intent(this,OperationActivity.class);
        startActivity(intent);

    }

}
Student.java程式碼:
package com.hfut.litepaldemo;

import org.litepal.crud.DataSupport;

/**
 * author:why
 * created on: 2018/3/18 14:09
 * description:
 */
public class Student extends DataSupport{
    private String name;
    private int age;
    private String sex;
    private int stuID;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getStuID() {
        return stuID;
    }

    public void setStuID(int stuID) {
        this.stuID = stuID;
    }

//    @Override
//    public String toString() {
//        return "stuID="+stuID+";name="+name+";age="+age+";sex="+sex;
//    }
}

OperationActivity.java程式碼:

package com.hfut.litepaldemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

public class OperationActivity extends AppCompatActivity {

    private static final String TAG = "OperationActivity";
    private TextView queryResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operation);
        queryResult=findViewById(R.id.queryResult);

    }


    public void insertData(View view) {
        Intent intent=new Intent(this,AddDataActivity.class);
        startActivity(intent);
        //Log.i(TAG, "insertData: 執行了新增資料操作");
    }

    public void deleteData(View view) {
       Intent intent=new Intent(this,DeleteDateActivity.class);
       startActivity(intent);
    }

    //把所有姓名為xiaoming的student的性別改為男性
    public void updateData(View view) {
        Student why = new Student();
        why.setSex("male");
        why.updateAll("name=?", "xiaoming");
        Log.i(TAG, "updateData: 執行了更新資料操作");
    }
    public void queryData(View view) {
        Log.i(TAG, "queryData: 執行了查詢資料操作");
        BufferedWriter bufferedWriter = null;
        OutputStream outputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("student表中的資料:\n");
        List<Student> students = DataSupport.findAll(Student.class);
        for (Student student : students) {
            stringBuilder.append("stuID=" + student.getStuID() + ";name=" + student.getName() + ";age=" + student.getAge() + ";sex=" + student.getSex() + "\n");
        }
        String tempString = stringBuilder.toString();
        queryResult.setText(tempString);
        try {
            outputStream = openFileOutput("students.txt", MODE_PRIVATE);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            Log.i(TAG, "queryData: "+tempString);
            bufferedWriter.write(tempString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

AddDataActivity.java程式碼:

package com.hfut.litepaldemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddDataActivity extends AppCompatActivity {
    private static final String TAG = "AddDataActivity";

    private EditText stuID;
    private EditText name;
    private EditText age;
    private EditText sex;
    private TextView addInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_data);
        initUI();
    }

    private void initUI() {
        stuID = findViewById(R.id.add_stuID);
        age = findViewById(R.id.add_age);
        name = findViewById(R.id.add_name);
        sex = findViewById(R.id.add_sex);
        addInfo = findViewById(R.id.addDataInfo);
    }


    public void addStudent(View view) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("stuID:" + stuID.getText().toString()+"  ").append("name:" + name.getText().toString()+"  ")
                .append("age:" + age.getText().toString()+"  ").append("sex:" + sex.getText().toString()+"  ").append("新增時間:"+new Date().toString());
        Student student = new Student();
        student.setStuID(Integer.parseInt(stuID.getText().toString()));
        student.setSex(sex.getText().toString());
        student.setAge(Integer.parseInt(age.getText().toString()));
        student.setName(name.getText().toString());
        student.save();
        Log.i(TAG, "addStudent: 執行了新增資料的操作");
        stuID.setText("");
        name.setText("");
        age.setText("");
        sex.setText("");
        addInfo.append("\n成功新增一條資料:\n");
        addInfo.append(stringBuilder.toString());
    }

    public void backup(View view) {
        addInfo.setText("");
        finish();
    }

}

DeleteDataActivity.java程式碼:

package com.hfut.litepaldemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.util.Date;

public class DeleteDateActivity extends AppCompatActivity {
    private EditText stuID;
    private EditText name;
    private EditText age;
    private EditText sex;
    private TextView deleteDataInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_date);
        initUI();
    }

    private void initUI() {
        stuID = findViewById(R.id.delete_stuID);
        name = findViewById(R.id.delete_name);
        age = findViewById(R.id.delete_age);
        sex = findViewById(R.id.delete_sex);
        deleteDataInfo=findViewById(R.id.deleteData);
    }

    public void deleteStudent(View view) {
        StringBuilder stringBuilder=new StringBuilder();
        Integer studentID = null;
        String studentName = null;
        String studentSex = null;
        Integer studentAge = null;
        if (!TextUtils.isEmpty(stuID.getText().toString())) {
            studentID = Integer.parseInt(stuID.getText().toString());
        }
        if (!TextUtils.isEmpty(name.getText().toString())) {
            studentName = name.getText().toString();
        }
        if (!TextUtils.isEmpty(sex.getText().toString())) {
            studentSex = sex.getText().toString();
        }
        if (!TextUtils.isEmpty(age.getText().toString())) {
            studentAge = Integer.parseInt(age.getText().toString());
        }

        if (studentID != null) {
            System.out.println("studentID:" + studentID);
            DataSupport.deleteAll(Student.class, "stuID=?", stuID.getText().toString());
            stringBuilder.append("您剛剛刪除了ID為"+studentID+"的學生資訊");
        } else {
            if (!TextUtils.isEmpty(studentName)) {
                if (!TextUtils.isEmpty(studentSex)) {
                    if (!TextUtils.isEmpty(age.getText().toString())) {
                        DataSupport.deleteAll(Student.class, "name=? and sex=? and age=?", studentName, studentSex, age.getText().toString());
                        stringBuilder.append("您剛剛刪除了姓名為"+studentName+";性別為"+studentSex+";年齡為"+studentAge+"的所有學生資訊");
                    } else {
                        DataSupport.deleteAll(Student.class, "name=? and sex=?", studentName, studentSex);
                        stringBuilder.append("您剛剛刪除了姓名為"+studentName+";性別為"+studentSex+"的所有學生資訊");
                    }

                } else {
                    if (!TextUtils.isEmpty(age.getText().toString())) {
                        DataSupport.deleteAll(Student.class, "name=? and age=?", studentName, age.getText().toString());
                        stringBuilder.append("您剛剛刪除了姓名為"+studentName+";年齡為"+studentAge+"的所有學生資訊");
                    } else {
                        DataSupport.deleteAll(Student.class, "name=?", studentName);
                        stringBuilder.append("您剛剛刪除了姓名為"+studentName+"的所有學生資訊");
                    }
                }
            } else {
                if (!TextUtils.isEmpty(studentSex)) {
                    if (!TextUtils.isEmpty(age.getText().toString())) {
                        DataSupport.deleteAll(Student.class, "sex=? and age=?", studentSex, age.getText().toString());
                        stringBuilder.append("您剛剛刪除了性別為"+studentSex+";年齡為"+studentAge+"的所有學生資訊");
                    } else {
                        DataSupport.deleteAll(Student.class, "sex=?", studentSex);
                        stringBuilder.append("您剛剛刪除了性別為"+studentSex+"的所有學生資訊");
                    }

                } else {
                    if (!TextUtils.isEmpty(age.getText().toString())) {
                        DataSupport.deleteAll(Student.class, "age=?", age.getText().toString());
                        stringBuilder.append("您剛剛刪除了年齡為"+studentAge+"的所有學生資訊");
                    } else {
                        Toast.makeText(this, "請輸入要刪除的資料", Toast.LENGTH_SHORT).show();
                        //DataSupport.deleteAll(Student.class, "name=?", studentName);
                    }

                }
            }
        }
        stringBuilder.append(";刪除時間"+new Date().toString());
        deleteDataInfo.append(stringBuilder.toString()+"\n");
        stuID.setText("");
        name.setText("");
        age.setText("");
        sex.setText("");
    }

    public void deleteAllData(View view) {
        DataSupport.deleteAll(Student.class);
    }

    public void backup(View view) {
        deleteDataInfo.setText("");
        finish();
    }
}

activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.hfut.litepaldemo.MainActivity">

    <Button
        android:textSize="15dp"
        android:layout_marginTop="20dp"
        android:text="建立資料庫"
        android:onClick="createDB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:textSize="15dp"
        android:layout_marginTop="10dp"
        android:text="更新資料庫"
        android:onClick="updateDB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:textSize="15dp"
        android:layout_marginTop="10dp"
        android:text="操作資料庫"
        android:onClick="operateDB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_operation.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.litepaldemo.OperationActivity">

<Button
    android:textSize="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:onClick="insertData"
    android:text="新增資料" />

<Button
    android:textSize="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:onClick="deleteData"
    android:text="刪除資料" />

<Button
    android:textSize="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:onClick="updateData"
    android:text="更新資料" />

<Button
    android:textSize="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:onClick="queryData"
    android:text="查詢資料" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:textSize="15dp"
        android:layout_marginTop="15dp"
        android:id="@+id/queryResult"
        android:hint="查詢結果"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</ScrollView>

</LinearLayout>

activity_add_data.xml程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.hfut.litepaldemo.AddDataActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="200dp"
            android:orientation="vertical">

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="stuID" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="name" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="age" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="sex" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical">
            <EditText
                android:id="@+id/add_stuID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/add_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/add_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/add_sex"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addStudent"
            android:text="新增" />

        <Button
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="backup"
            android:text="退出" />

    </LinearLayout>


    <TextView
        android:textSize="15dp"
        android:id="@+id/addDataInfo"
        android:hint="新增結果資訊"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

activity_delete_data.xml程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.hfut.litepaldemo.DeleteDateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="60dp"
            android:layout_height="200dp"
            android:orientation="vertical">

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="stuID" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="name" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="age" />

            <TextView
                android:textSize="20dp"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="sex" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical">
            <EditText
                android:id="@+id/delete_stuID"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/delete_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/delete_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/delete_sex"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="deleteStudent"
            android:text="刪除" />

        <Button
            android:layout_marginLeft="20dp"
            android:onClick="deleteAllData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刪除所有"/>
        <Button
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="backup"
            android:text="退出" />

    </LinearLayout>

    <TextView
        android:textSize="15dp"
        android:hint="刪除結果資訊"
        android:id="@+id/deleteData"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

build.gradle檔案程式碼:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.hfut.litepaldemo"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'org.litepal.android:core:1.4.1'
}

litepal.xml程式碼:

<?xml version="1.0" encoding="utf-8" ?>
<litepal>
    <dbname value="School">

    </dbname>
    <version value="2">
    </version>

    <!--指定對映模型-->
    <list>
        <mapping class="com.hfut.litepaldemo.Student">

        </mapping>
        <mapping class="com.hfut.litepaldemo.Teacher">

        </mapping>
    </list>
</litepal>

3,執行結果

第一步:執行程式

第二步:點選“建立資料庫”按鈕

第三步:點選“操作資料庫”按鈕

第四步:點選“查詢資料”按鈕

第五步:點選“新增資料”按鈕

第六步:輸入新增資料,然後點選“新增”按鈕,多新增幾條資料

第七步:點選“退出”按鈕,在點選“查詢資料”按鈕

第八步:點選“刪除資料”按鈕

第九步:編輯要刪除資料資訊,點選“刪除”按鈕

第十步:點選“全部刪除”按鈕,退出,點選“查詢資料”按鈕

總結:這篇是關於資料持久化中最後一篇了;LitePal很強大,我這是九牛一毛的深度;我的示例程式碼中,關於資料更新還沒有做,有興趣的朋友可以完善一下,因為我這裡面的stuID定義為主鍵,所以有唯一性,這樣我在邏輯判斷的時候也考慮了這一點,所以在已知stuID的情況下,刪除資訊就不需要其他資訊了。

相關推薦

第一程式碼第二 學習總結15 資料持久化LitePal開源基本使用

      最近利用下班時間,找了看什麼書比較適合初學android的朋友,很多人推薦了這本書,於是就買了一本,感覺看書,思考,動手,再思考和總結這樣過程還是很有必要的,於是就打算把自己學習的東西簡單的總結一下;方便自己以後查詢,也有利於學習的鞏固。在這裡首先要感謝一下書籍的

Android 第一程式碼(第二)學習筆記(一)

onCreate()  活動第一次被建立的時候呼叫; onStart()   活動將要展示在前端時呼叫 onResume()  處於執行狀態,並且可以跟使用者互動 onPause()   活動即將從前臺退出 onStop()  活動完全不可見 onDestroy()   被

Android 第一程式碼(第二)學習筆記

本人電腦是Mac mini 4G記憶體,真的小的可以,而且還是不能加記憶體條的那種,不過勉強還是能用。 開發工具用的Android studio 2.3  ,首先Android studio 2.3 官方建議在Mac下記憶體不低於3G,不過 我分配了2點多G用起來出來慢些

第一程式碼Android》學習總結第二章 Activity建立與相關設定

一、id標籤 如果在XML檔案中引用一個id,則使用@id/id_name; 如果在XML檔案中定義一個id,則使用@+id/id_name。 二、程式中設定主活動 在AndroidMaifest.xml中設定 <intent-filter>   

第一程式碼Android》學習總結第三章 自定義佈局與控制元件

1、View是Android中最基本的元件,它可以在螢幕上繪製一塊矩形區域,並在這塊區域內響應各種事件。所有控制元件都直接或間接繼承自View。 2、ViewGroup是一種特殊的View,可以包含很多子View和子ViewGroup,是一個用於放置控制元件和佈局的容器。所有佈局都直接或間

第一程式碼Android》學習總結第三章 常用控制元件使用方法

1、TextView match_parent:表示讓父佈局決定當前控制元件大小,當前控制元件大小與父佈局大小一樣。 wrap_content:表示讓空間內容決定當前控制元件大小讓當前控制元件大小能夠剛好包含住控制元件內的內容。         對控

第一程式碼Android》學習總結第四章 廣播機制實踐——強制下線功能

        強制下線功能需要在任何一個介面上彈出一個對話方塊,讓使用者必須點選對話方塊中的確定按鈕,關閉所有活動,然後回到登入介面即可。            1、建立ActivityCo

第一程式碼Android》學習總結第五章 詳解廣播機制

一、廣播機制簡介         Android提供了一系列API,允許程式自由的傳送和接收廣播,同時每個程式都可以對自己感興趣的廣播進行註冊,該程式便可以只接受來自於系統或其他應用程式的自己關心的廣播內容。 標準廣播:    

第一程式碼Android》學習總結第四章 Fragment應用實踐

Fragment應用實踐-----簡易新聞應用佈局(可同時相容手機與平板) 1、在app/build.gradle新增依賴庫 compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' 2、新建News新聞實體類 publi

第一程式碼Android》學習總結第四章 Fragment的執行狀態與生命週期

一、Fragment四種狀態 1、執行狀態 當一個Fragment是可見的,同時它所關聯的Activity正處於執行狀態,則該Fragment也處於執行狀態。 2、暫停狀態 當一個Activity處於暫停狀態,與它關聯的可見碎片就會處於暫停狀態。 3、停止狀態  &

第一程式碼Android》學習總結第四章 Fragment的簡單介紹

        碎片(Fragment)是一種可以嵌入在活動當中的UI片段,他能讓程式更加合理與充分的利用螢幕空間。 一、Fragment的簡單使用 1、新建左側Fragment佈局left_fragment.xml與右側Fragment佈局right

FC 12 第一程式碼Material Design學習總結

Material Design簡介 我為什麼把這一章分成好幾個文章來寫? 遇到的問題及解決方法 這兩天寫的文章的目錄 Material Design 簡介 Material Design 是由Google的設計工程師們基於傳統優秀的設計原則,給豐富的創意和科學

第一程式碼第二第五章的傳送標準廣播小節程式執行未出結果的原因

按著書上的指導把程式碼寫完之後,執行程式,再點選一下Send Broadcast按鈕,卻發現死活彈不出我們在程式碼寫的received in MyBroadcastReceiver這條提示,瞬間鬱悶了,當時想著是不是作者的程式碼沒寫對呢?結果網上各種找原因,終於找到了:一般大

第一程式碼第二ListView的使用(listView複用遇到的坑)

這個複雜又好用的控制元件,說不清是愛是恨。開始學習它吧。 首先當然是建立專案啊,然後修改一下activity_main的佈局檔案,內容如下 <?xml version="1.0" encoding="utf-8"?> <LinearLa

Android第一程式碼第二:5.3.2傳送有序廣播,在安卓8上無法接收廣播我個人的處理方法

希望有大佬能教我別的方法,我明前只能想到這個方法啦。多謝! 書上的原始碼是 Intent intent = new Intent("com.example.weiru.broadcasttest.MY_BROADCAST") ; sendOrderedBroadcast

第一程式碼第二 學習總結17 ContentProvider學習總結 Part2

        最近利用下班時間,找了看什麼書比較適合初學android的朋友,很多人推薦了這本書,於是就買了一本,感覺看書,思考,動手,再思考和總結這樣過程還是很有必要的,於是就打算把自己學習的東西簡單的總結一下;方便自己以後查詢,也有利於學習的鞏固。在這裡

第一程式碼第二 學習總結16 ContentProvider學習總結 Part1

       最近利用下班時間,找了看什麼書比較適合初學android的朋友,很多人推薦了這本書,於是就買了一本,感覺看書,思考,動手,再思考和總結這樣過程還是很有必要的,於是就打算把自己學習的東西簡單的總結一下;方便自己以後查詢,也有利於學習的鞏固。在這裡首

第一程式碼第二 學習總結結束篇

      最近利用下班時間,找了看什麼書比較適合初學android的朋友,很多人推薦了這本書,於是就買了一本,感覺看書,思考,動手,再思考和總結這樣過程還是很有必要的,於是就打算把自己學習的東西簡單的總結一下;方便自己以後查詢,也有利於學習的鞏固。在這裡首先要感謝一下書籍的

第一程式碼-運用手機多媒體總結(下)

從相簿中選擇照片 第一步,開啟相簿 //判斷是否有許可權,沒有就申請開啟相簿的許可權,有則開啟相簿 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_

Android Studio 手動建立活動(Activity) 第一程式碼 第二

活動概念:是一種可以包含使用者介面的元件,主要用於和使用者進行互動 手動建立活動:新建一個Android專案,專案名為ActivityTest,包名使用預設值com.example.activitytest 1.新建Android Studio專案 點選Start a