目录

  • 第1章 Android UI设计
    • ● 版本与程序
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例1-1 线性布局应用示例
    • ● 例1-2 帧布局应用示例
    • ● 例1-3 表格布局应用示例
    • ● 例1-4 相对布局应用示例
    • ● 例1-5 网格布局应用示例
    • ● 例1-6 约束布局应用示例
    • ● 例1-7 文本标签组件示例
    • ● 例1-8 制作登陆界面
    • ● 例1-9 进度条ProgressBar应用示例
    • ● 例1-10 单选按钮与复选按钮
    • ● 例1-11 简单列表示例
    • ● 例1-12 ListActivity示例
    • ● 例1-13 带有图片的列表
    • ● 例1-14 没有数据的ListView
    • ● 例1-15 改进的列表
    • ● 实战演练——生肖背后的故事
  • 第2章 Activity与多个用户界面
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例2-1 从Activity启动另一个Activity示例
    • ● 例2-2 传递数据到第二个Activity示例
    • ● 例2-3 消息提示Toast示例
    • ● 例2-4 消息对话框示例
    • ● 例2-5 选项菜单应用示例
    • ● 例2-6 上下文菜单应用示例
    • ● 实战演练——BMI体质指数计算器
  • 第3章 多媒体播放与录制
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例3-1 音乐播放器, 播放项目资源中的音乐
    • ● 例3-2 音乐播放器, 播放SD卡中的音乐
    • ● 例3-3 应用MediaPlayer设计视频播放器
    • ● 例3-4 应用VidioView设计视频播放器
    • ● 例3-5 录音示例
    • ● 例3-6 拍照示例
    • ● 实战演练——音乐播放器
  • 第4章    广播与服务
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例4-1 消息广播程序示例
    • ● 例4-2 系统通知服务示例
    • ● 例4-3 广播和时钟服务示例
    • ● 例4-4 调用系统功能拨打电话
    • ● 例4-5 后台音乐服务示例
    • ● 实战演练——播放后台音乐
  • 第5章    数据存储
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例5-1 内部存储文件示例
    • ● 例5-2 SD文件示例
    • ● 例5-3 JSON数据示例
    • ● 例5-4 SharedPreferences示例
    • ● 例5-5 创建与删除数据库示例
    • ● 例5-6 数据库记录的操作示例
    • ● 实战演练——掌上日记本
  • 第6章    图像和动画
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例6-1 绘制几何图形示例
    • ● 例6-2 补间动画示例
    • ● 例6-3 属性动画示例
    • ● 例6-4 ImageView图像浏览示例
    • ● 例6-5 ImageSwitcher展示相册示例
    • ● 例6-6 GridView展示相册示例
    • ● 例6-7 游戏中触屏事件示例
    • ● 实战演练——图片与动画
  • 第7章    网络编程
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例7-1 应用WebView的对象浏览网页
    • ● 例7-2 从Web服务器读取图像文件
    • ● 例7-3 读取JSON数据
    • ● 例7-4 解析JSON数据
    • ● 实战演练——城市天气预报
  • 第8章    分享美食
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章知识点讲解
    • ● 项目制作流程演示
    • ● 示范1 图标SVG
    • ● 示范2 卡片视图
    • ● 示范3 导航视图
  • 课程资源
    • ● 学生资源包
    • ● 网站地址
例5-1 内部存储文件示例

【例5-1】 读取与写入文件示例

 将数据以文件方式存储到设备的内部,其存储的文件被所创建的应用程序私有。该文件默认位于data/data/<packagename>/files/目录下,如果其他应用程序要操作该文件,需要获得权限。当应用程序被卸载时,其内部存储文件也随之被删除。

教学视频


打开values下的strings.xml,添加属性为“hello”的元素的内容:

<resources>

    <string name="app_name">Chap05</string>

    <string name="hello"> \n青春不是年华,而是心境;

        \n青春不是桃面、丹唇、柔膝,而是深沉的意志、恢宏的想像、炽热的感情;

        \n青春是生命的深泉涌流。

    </string>

</resources>

布局文件activity_file.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:id="@+id/activity_share_preference"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:layout_marginTop="20dp"

    android:weightSum="1">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">


        <Button

            android:id="@+id/btn_Save"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_marginLeft="5dp"

            android:layout_weight="1"

            android:onClick  ="Click"

            android:text="存入file"

            android:textSize="24sp" />


        <Button

            android:id="@+id/btn_Get"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_marginLeft="5dp"

            android:layout_weight="1"

            android:onClick  ="Click"

            android:text="取出file"

            android:textSize="24sp" />


    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:layout_marginTop="20dp">


    </LinearLayout>


    <TextView

        android:id="@+id/txt"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="0.44"

        android:textSize="24sp" />

</LinearLayout>


控制文件FileActivity.java源代码

package com.example.chap05;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;


public class FileActivity extends AppCompatActivity {

    Button btn_Save,btn_Get;

    TextView txt;

    static final String filename ="test2.txt";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_file);

        btn_Save = (Button)findViewById(R.id.btn_Save);

        btn_Get = (Button)findViewById(R.id.btn_Get);

        txt = (TextView)findViewById(R.id.txt);

        btn_Save.setOnClickListener(new mClick11());

        btn_Get.setOnClickListener(new mClick12());

    }

    //存入file,写文件

    class mClick11 implements View.OnClickListener {

        @Override

        public void onClick(View view) {

            String str = getString(R.string.hello);

            FileOutputStream f_out;

            try{

                f_out=openFileOutput(filename, Context.MODE_PRIVATE);

                f_out.write(str.getBytes());

                f_out.close();

                txt.setText("写文件\n"+str);

            }catch (IOException e){

                e.printStackTrace();

                txt.setText("写文件异常");}

        }

    }

    //取出file,读文件

    class mClick12 implements View.OnClickListener{


        @Override

        public void onClick(View view) {

            FileInputStream f_in;

            try{

                f_in=openFileInput(filename);

                byte[] buffer = new byte[f_in.available()];   //available()取得文件总字节数

                f_in.read(buffer);

                String str = new String(buffer);

                txt.setText("读文件\n"+str);

            }catch (IOException e){

                e.printStackTrace();

                txt.setText("读文件异常");

            }

        }

    }

}


打开项目配置文件AndroidManifest.xml,修改工程要启动的Activity类名

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.chap05">


    <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/AppTheme">

        <activity android:name=".FileActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>