wy168 发表于 2022-10-25 19:41:42

使用Android studio制作一个APP?完整版教学


    <h1 style="text-align: left; margin-bottom: 10px;">架构组成</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">在JetBrains Intellij IDEA的基础上,Android Studio 提供:</span></p><span style="color: green;">基于Gradle的构建支持</span><span style="color: green;">Android 专属的重构和快速修复</span><span style="color: green;">提示工具以捕获性能、可用性、版本兼容性等问题</span><span style="color: green;">支持ProGuard 和应用签名</span><span style="color: green;">基于模板的向导来生成常用的 Android 应用设计和组件</span><span style="color: green;">功能强大的布局编辑器,可以让你拖拉 UI 控件并进行效果预览</span>
    <h1 style="text-align: left; margin-bottom: 10px;">功能介绍</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">1、优化小贴士:在主体中打开你的应用,点击小贴士,会得到这样的建议:为你的应用开发平板电脑版本。</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">2、应用翻译服务:允许开发者直接在开发主体中获得专业的翻译。上传你的需求,选择翻译,其会显示翻译方和价格,并在一周内发回译本。</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">3、推荐跟踪:允许开发者找出最有效的广告</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">4、营收曲线图:向开发者展示其应用营收,以国家进行划分</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">5、试用版测试和阶段性展示:开发者可以对应用进行测试,然后向测试用户推出,测试结果不会对外公布。当一个版本的测试结束,开发者可以向特定比例用户推出 。</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">Android Studio这款开发工具被首次公布,这也是为了方便开发者基于Android开发。</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">一个小APP开发实战“笔记本”</h1>
    <div style="text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/ef3182a5090740e686938cf8607a38ac~noop.image?_iz=58558&amp;from=article.pc_detail&amp;x-expires=1664554637&amp;x-signature=u4Wnqhxc7wxBBVSMvida5lCv9TA%3D" style="width: 100%; margin-bottom: 20px;"></div>
    <h1 style="text-align: left; margin-bottom: 10px;">一、需求分析</h1>
    <h1 style="text-align: left; margin-bottom: 10px;">1.业务需求分析:</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">分析潜在客户需要什么产品或服务</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">2.架构分析:</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">分析所做的产品或服务需要什么功能及实现⽅式并画出功能结构图,记事本案例如下</span></p>
    <div style="text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/c7eb38339f28421b940b17838cdf810a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;x-expires=1664554637&amp;x-signature=Zcjm9O2HnL9AJA33m0HS7sloTLk%3D" style="width: 100%; margin-bottom: 20px;"></div>
    <h1 style="text-align: left; margin-bottom: 10px;">3.数据库类设计分析:</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">数据库设计是项⽬开发中⾮常关键的⼀个环节。同样在记事本案例中也⾄关重要,我们通过数据库表(Note)进⾏增删改查操作,记事本的数据表如下所⽰</span></p>
    <div style="text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/14b5a96857be4a9fb76db1f89f28d3b8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;x-expires=1664554637&amp;x-signature=OeAxJZgjCpabIp1B1lfwdXBbmaQ%3D" style="width: 100%; margin-bottom: 20px;"></div>
    <h1 style="text-align: left; margin-bottom: 10px;">4.界⾯需求分析:</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">友好的界⾯在移动平台开发中⾮常重要,也是⽤户使⽤软件的先决条件。记事本案例分为3个界⾯,分别为记事本界⾯、添加界⾯和修改界⾯ 记事本界⾯包含添加按钮和记录列表,点击后会跳转到添加界⾯,界⾯标题为添加记录,也可在该界⾯清除和保存编辑的内容。</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">点击记事本界⾯中的Item时,会跳转到修改记录界⾯,界⾯标题为修改记录,在该界⾯中可以查看和修改已保存的记录内容,也可清除和保存编辑的 内容</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">二、本案例步骤</h1>
    <h1 style="text-align: left; margin-bottom: 10px;">1.创建项⽬后将Activity名称设置为NotepadActivity,</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">布局⽂件为activity_notepad并将所需素材add.png(添加按钮),save_note.png(保存按钮),delete.png(删除按钮),back.png(返回按钮)导⼊drawable⽂件夹中,如下</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">上⾯添加按钮右边还有个返回按钮图⽚是⽩⾊的</span></p>
    <div style="text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/f6060762156441f5b65aeb02b2485bc5~noop.image?_iz=58558&amp;from=article.pc_detail&amp;x-expires=1664554637&amp;x-signature=xFx3BTziKpRMmP%2FNEk6hfxk1aVM%3D" style="width: 100%; margin-bottom: 20px;"></div>
    <h1 style="text-align: left; margin-bottom: 10px;">2.编写activity_notepad.xml布局⽂件,</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">放置⼀个TextView控件⽤于显⽰界⾯标题,⼀个ListView控件⽤于显⽰记录列表,⼀个ImageView控件⽤于显⽰添加按钮的图⽚</span></p><span style="color: green;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">RelativeLayout</span> <span style="color: green;">xmlns:android</span>=<span style="color: green;">"http://schemas.android.com/apk/res/android"</span>
      <span style="color: green;">xmlns:tools</span>=<span style="color: green;">"http://schemas.android.com/tools"</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:background</span>=<span style="color: green;">"#fefefe"</span>
      <span style="color: green;">tools:context</span>=<span style="color: green;">".NotepadActivity"</span>&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">TextView</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"45dp"</span>
      <span style="color: green;">android:gravity</span>=<span style="color: green;">"center"</span>
      <span style="color: green;">android:text</span>=<span style="color: green;">"记事本"</span>
      <span style="color: green;">android:textSize</span>=<span style="color: green;">"20sp"</span>
      <span style="color: green;">android:textColor</span>=<span style="color: green;">"@android:color/white"</span>
      <span style="color: green;">android:textStyle</span>=<span style="color: green;">"bold"</span>
      <span style="color: green;">android:background</span>=<span style="color: green;">"#fb7a6a"</span>
      <span style="color: green;">android:id</span>=<span style="color: green;">"@+id/note_name"</span>
      /&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">ListView</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:id</span>=<span style="color: green;">"@+id/listview"</span>
      <span style="color: green;">android:cacheColorHint</span>=<span style="color: green;">"#00000000"</span>
      <span style="color: green;">android:divider</span>=<span style="color: green;">"#B4B4B4"</span>
      <span style="color: green;">android:dividerHeight</span>=<span style="color: green;">"1dp"</span>
      <span style="color: green;">android:fadingEdge</span>=<span style="color: green;">"none"</span>
      <span style="color: green;">android:listSelector</span>=<span style="color: green;">"#00000000"</span>
      <span style="color: green;">android:scrollbars</span>=<span style="color: green;">"none"</span>
      <span style="color: green;">android:layout_below</span>=<span style="color: green;">"@id/note_name"</span>
      /&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">ImageView</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"wrap_content"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"wrap_content"</span>
      <span style="color: green;">android:src</span>=<span style="color: green;">"@drawable/add"</span>
      <span style="color: green;">android:id</span>=<span style="color: green;">"@+id/add"</span>
      <span style="color: green;">android:layout_marginBottom</span>=<span style="color: green;">"30dp"</span>
      <span style="color: green;">android:layout_alignParentBottom</span>=<span style="color: green;">"true"</span>
      <span style="color: green;">android:layout_centerHorizontal</span>=<span style="color: green;">"true"</span>
      /&gt;</span>
    <span style="color: green;">&lt;/<span style="color: green;">RelativeLayout</span>&gt;</span>
    <h1 style="text-align: left; margin-bottom: 10px;">3.修改清单⽂件</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">项⽬创建后所有界⾯都有⼀个默认的标题栏,该标题栏不太美观,因此需要在清单⽂件(AndroidManifest.xml)中的标签中</span><span style="color: green;">&lt;application&gt;</span>修改android:theme属性</p>android:theme=<span style="color: green;">"@style/Theme.AppCompat.NoActionBar"</span>
    <h1 style="text-align: left; margin-bottom: 10px;">4.搭建记事本界⾯Item布局</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">在res/layout⽂件夹中,创建⼀个布局⽂件notepad_item_layout.xml,在其中放置两个TextView控件,分别⽤来显⽰记录的部分内容与保存记录的时间</span></p>?xml version="1.0" encoding="utf-8"?&gt;
    <span style="color: green;">&lt;<span style="color: green;">LinearLayout</span> <span style="color: green;">xmlns:android</span>=<span style="color: green;">"http://schemas.android.com/apk/res/android"</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:orientation</span>=<span style="color: green;">"vertical"</span>
      <span style="color: green;">android:paddingLeft</span>=<span style="color: green;">"12dp"</span>&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">TextView</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"wrap_content"</span>
      <span style="color: green;">android:id</span>=<span style="color: green;">"@+id/item_content"</span>
      <span style="color: green;">android:maxLines</span>=<span style="color: green;">"2"</span>
      <span style="color: green;">android:ellipsize</span>=<span style="color: green;">"end"</span>
      <span style="color: green;">android:lineSpacingExtra</span>=<span style="color: green;">"3dp"</span>
      <span style="color: green;">android:paddingTop</span>=<span style="color: green;">"10dp"</span>
      <span style="color: green;">android:textColor</span>=<span style="color: green;">"@android:color/black"</span>
      /&gt;</span>
    <span style="color: green;">&lt;<span style="color: green;">TextView</span>
      <span style="color: green;">android:layout_width</span>=<span style="color: green;">"match_parent"</span>
      <span style="color: green;">android:layout_height</span>=<span style="color: green;">"wrap_content"</span>
      <span style="color: green;">android:id</span>=<span style="color: green;">"@+id/item_time"</span>
      <span style="color: green;">android:textColor</span>=<span style="color: green;">"#fb7a6a"</span>
      <span style="color: green;">android:paddingTop</span>=<span style="color: green;">"5dp"</span>
      <span style="color: green;">android:paddingBottom</span>=<span style="color: green;">"7dp"</span>
      /&gt;</span>
    <span style="color: green;">&lt;/<span style="color: green;">LinearLayout</span>&gt;</span>
    <h1 style="text-align: left; margin-bottom: 10px;">5.封装记录信息实体类</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">由于记事本中的每个记录都会有记录内容和保存记录的时间属性,因此需要创建⼀个NotepadBean⽤于存放这些属性。选中你所创建的项⽬的包,右击选择New&gt;Package,创建⼀个bean包,在该包中创建⼀NotepadBean类,该类中定义记录信息的所有属性</span></p>package com.example.notepad.bean;
    <span style="color: green;">public</span> <span style="color: green;">class</span> NotepadBean {
    <span style="color: green;">private</span> <span style="color: green;">String</span> id;
    <span style="color: green;">private</span> <span style="color: green;">String</span> notepadContent;
    <span style="color: green;">private</span> <span style="color: green;">String</span> notepadTime;
    <span style="color: green;">public</span> <span style="color: green;">String</span> getId(){
    returnid;
    }
    <span style="color: green;">public</span> <span style="color: green;">void</span> setId(<span style="color: green;">String</span> id){
    <span style="color: green;">this</span>.id=id;
    }
    <span style="color: green;">public</span> <span style="color: green;">String</span> getNotepadContent(){
    <span style="color: green;">return</span> notepadContent;
    }
    <span style="color: green;">public</span> <span style="color: green;">void</span> setNotepadContent(<span style="color: green;">String</span> notepadContent){
    <span style="color: green;">this</span>.notepadContent=notepadContent;
    }
    <span style="color: green;">public</span> <span style="color: green;">String</span> getNotepadTime(){
    <span style="color: green;">return</span> notepadTime;
    }
    <span style="color: green;">public</span> <span style="color: green;">void</span> setNotepadTime(<span style="color: green;">String</span> notepadTime){
    <span style="color: green;">this</span>.notepadTime=notepadTime;
    }
    }<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">6.编写记事本界⾯列表适配器</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">由于记事本界⾯的记录列表是使⽤ListView控件来展⽰的,因此需要创建⼀个数据适配器NotepadAdapter对ListView控件进⾏数据适配。步骤如下 选中你所创建的项⽬的包,右击选择New&gt;Package,创建⼀个adapter包,在adapter包中创建⼀个NotepadAdapter类继承⾃BaseAdapter类,并重写getCount(),getItem(),getItemId(),getView()⽅法,这些⽅法⽤于获取Item总数、对应Item对象、Item对象的Id、对应的Item视图,在NotepadAdapter类中创建⼀个ViewHolder类,在该类中初始化Item界⾯中的控件,具体代码如下</span></p><span style="color: green;">package</span> com.example.notepad.adapter;
    <span style="color: green;">import</span> android.content.Context;
    <span style="color: green;">import</span> android.view.LayoutInflater;
    <span style="color: green;">import</span> android.view.View;
    <span style="color: green;">import</span> android.view.ViewGroup;
    <span style="color: green;">import</span> android.widget.BaseAdapter;
    <span style="color: green;">import</span> android.widget.TextView;
    <span style="color: green;">import</span> com.example.notepad.R;
    <span style="color: green;">import</span> com.example.notepad.bean.NotepadBean;
    <span style="color: green;">import</span> java.util.List;
    <span style="color: green;">public</span> <span style="color: green;"><span style="color: green;">class</span> <span style="color: green;">NotepadAdapter</span> <span style="color: green;">extends</span> <span style="color: green;">BaseAdapter</span> </span>{
    <span style="color: green;">private</span> LayoutInflater layoutInflater;
    <span style="color: green;">private</span> List&lt;NotepadBean&gt; list;
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">NotepadAdapter</span><span style="color: green;">(Context context, List&lt;NotepadBean&gt;list)</span></span>{
    <span style="color: green;">this</span>.layoutInflater=LayoutInflater.from(context);
    <span style="color: green;">this</span>.list=list;
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">int</span> <span style="color: green;">getCount</span><span style="color: green;">()</span></span>{
    <span style="color: green;">return</span> list==<span style="color: green;">null</span>?<span style="color: green;">0</span>:list.size();
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> Object <span style="color: green;">getItem</span><span style="color: green;">(<span style="color: green;">int</span> position)</span></span>{
    <span style="color: green;">return</span> list.get(position);
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">long</span> <span style="color: green;">getItemId</span><span style="color: green;">(<span style="color: green;">int</span> position)</span></span>{
    <span style="color: green;">return</span> position;
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> View <span style="color: green;">getView</span><span style="color: green;">(<span style="color: green;">int</span> position,View convertView, ViewGroup parent)</span></span>{
    ViewHolder viewHolder;
    <span style="color: green;">if</span>(convertView==<span style="color: green;">null</span>){
    convertView=layoutInflater.inflate(R.layout.notepad_item_layout,<span style="color: green;">null</span>);
    viewHolder=<span style="color: green;">new</span> ViewHolder(convertView);
    convertView.setTag(viewHolder);
    }<span style="color: green;">else</span>{
    viewHolder=(ViewHolder)convertView.getTag();
    }
    NotepadBean noteInfo=(NotepadBean)getItem(position);
    viewHolder.tvNoteoadContent.setText(noteInfo.getNotepadContent());
    viewHolder.tvNotepadTime.setText(noteInfo.getNotepadTime());
    <span style="color: green;">return</span> convertView;
    }
    <span style="color: green;"><span style="color: green;">class</span> <span style="color: green;">ViewHolder</span></span>{
    TextView tvNoteoadContent;
    TextView tvNotepadTime;
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">ViewHolder</span><span style="color: green;">(View view)</span></span>{
    tvNoteoadContent=(TextView)view.findViewById(R.id.item_content);
    tvNotepadTime=(TextView)view.findViewById(R.id.item_time);
    }
    }
    }<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">创建数据库</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">在记事本案例中增删改查记录的数据都是通过操作数据库完成的。因此需要创建数据库类SQLiteHelper与数据库的⼯具类DBUtils,步骤如下 选中你所创建的项⽬的包,右击选择New&gt;Package,创建⼀个utils包,在utils包中创建⼀个DBUtils类,在该类中定义数据库的名称、表名、数据库版本、数据库表中的列名以及获取当前⽇期等信息</span></p> <span style="color: green;">package</span> com.example.notepad.utils;
    <span style="color: green;">import</span> java.text.SimpleDateFormat;
    <span style="color: green;">import</span> java.util.Date;
    <span style="color: green;">public</span> <span style="color: green;"><span style="color: green;">class</span> <span style="color: green;">DBUtils</span> </span>{
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String DATABASE_NAME=<span style="color: green;">"Notepad"</span>;
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String DATAVASE_TABLE=<span style="color: green;">"Note"</span>;
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> <span style="color: green;">int</span> DATEBASE_VERION=<span style="color: green;">1</span>;
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String NOTEPAD_ID=<span style="color: green;">"id"</span>;
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String NOTEPAD_CONTENT=<span style="color: green;">"content"</span>;
    <span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String NOTEPAD_TIME=<span style="color: green;">"notetime"</span>;
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: green;">final</span> String <span style="color: green;">getTime</span><span style="color: green;">()</span></span>{
    SimpleDateFormat simpleDateFormat=<span style="color: green;">new</span> SimpleDateFormat(<span style="color: green;">"yyyy年MM⽉dd⽇HH:mm:ss"</span>);
    Date date=<span style="color: green;">new</span> Date(System.currentTimeMillis());
    <span style="color: green;">return</span> simpleDateFormat.format(date);
    }
    }<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名 选中你所创建的项⽬的包,右击选择New&gt;Package,创建⼀个database包,在database包中创建⼀个SQLiteHelper类继承⾃SQLiteOpenHelper类</span></p>package com.example.notepad.database;
    <span style="color: green;">import</span> android.content.ContentValues;
    <span style="color: green;">import</span> android.content.Context;
    <span style="color: green;">import</span> android.database.Cursor;
    <span style="color: green;">import</span> android.database.sqlite.SQLiteDatabase;
    <span style="color: green;">import</span> android.database.sqlite.SQLiteOpenHelper;
    <span style="color: green;">import</span> com.example.notepad.bean.NotepadBean;
    <span style="color: green;">import</span> com.example.notepad.utils.DBUtils;
    <span style="color: green;">import</span> java.util.ArrayList;
    <span style="color: green;">import</span> java.util.List;
    <span style="color: green;">public</span> <span style="color: green;">class</span> SQLiteHelper <span style="color: green;">extends</span> SQLiteOpenHelper {
    <span style="color: green;">private</span> SQLiteDatabase sqLiteDatabase;
    <span style="color: green;">public</span> SQLiteHelper(Context context){
    <span style="color: green;">super</span>(context,DBUtils.DATABASE_NAME,<span style="color: green;">null</span>,DBUtils.DATEBASE_VERION);
    sqLiteDatabase=<span style="color: green;">this</span>.getWritableDatabase();
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;">public</span> <span style="color: green;">void</span> onCreate(SQLiteDatabase db){
    db.execSQL(<span style="color: green;">"create table "</span>+DBUtils.DATAVASE_TABLE+<span style="color: green;">"( "</span>+DBUtils.NOTEPAD_ID+<span style="color: green;">" integer primary key autoincrement ,"</span>+DBUtils.NOTEPAD_CON
    TENT+<span style="color: green;">" text , "</span>+DBUtils.NOTEPAD_TIME+<span style="color: green;">" text ) "</span>);
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;">public</span> <span style="color: green;">void</span> onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){}
    <span style="color: green;">public</span> <span style="color: green;">boolean</span> insertData(<span style="color: green;">String</span> userContent,<span style="color: green;">String</span> userTime){
    ContentValues contentValues=<span style="color: green;">new</span> ContentValues();
    contentValues.put(DBUtils.NOTEPAD_CONTENT,userContent);
    contentValues.put(DBUtils.NOTEPAD_TIME,userTime);
    <span style="color: green;">return</span> sqLiteDatabase.insert(DBUtils.DATAVASE_TABLE,<span style="color: green;">null</span>,contentValues)&gt;<span style="color: green;">0</span>;
    }
    <span style="color: green;">public</span> <span style="color: green;">boolean</span> deteleData(<span style="color: green;">String</span> id){
    <span style="color: green;">String</span> sql=DBUtils.NOTEPAD_ID+<span style="color: green;">"=?"</span>;
    <span style="color: green;">String</span>[] contentValuesArray=<span style="color: green;">new</span> <span style="color: green;">String</span>[]{<span style="color: green;">String</span>.valueOf(id)};
    <span style="color: green;">return</span> sqLiteDatabase.delete(DBUtils.DATAVASE_TABLE,sql,contentValuesArray)&gt;<span style="color: green;">0</span>;
    }
    <span style="color: green;">public</span> <span style="color: green;">boolean</span> updateData(<span style="color: green;">String</span> id,<span style="color: green;">String</span> content,<span style="color: green;">String</span> userYear){
    ContentValues contentValues=<span style="color: green;">new</span> ContentValues();
    contentValues.put(DBUtils.NOTEPAD_CONTENT,content);
    contentValues.put(DBUtils.NOTEPAD_TIME,userYear);
    <span style="color: green;">String</span> sql=DBUtils.NOTEPAD_ID+<span style="color: green;">"=?"</span>;
    <span style="color: green;">String</span>[] strings=<span style="color: green;">new</span> <span style="color: green;">String</span>[]{id};
    <span style="color: green;">return</span> sqLiteDatabase.update(DBUtils.DATAVASE_TABLE,contentValues,sql,strings)&gt;<span style="color: green;">0</span>;
    }
    <span style="color: green;">public</span> List&lt;NotepadBean&gt;query(){
    List&lt;NotepadBean&gt; list=<span style="color: green;">new</span> ArrayList&lt;NotepadBean&gt;();
    Cursor cursor=sqLiteDatabase.query(DBUtils.DATAVASE_TABLE,<span style="color: green;">null</span>,<span style="color: green;">null</span>,<span style="color: green;">null</span>,<span style="color: green;">null</span>,<span style="color: green;">null</span>,DBUtils.NOTEPAD_ID+<span style="color: green;">" desc"</span>);
    <span style="color: green;">if</span>(cursor!=<span style="color: green;">null</span>){
    <span style="color: green;">while</span>(cursor.moveToNext()){
    NotepadBean noteInfo=<span style="color: green;">new</span> NotepadBean();
    <span style="color: green;">String</span> id=<span style="color: green;">String</span>.valueOf(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));
    <span style="color: green;">String</span> content=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT));
    <span style="color: green;">String</span> time=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME));
    noteInfo.setId(id);
    noteInfo.setNotepadContent(content);
    noteInfo.setNotepadTime(time);
    list.add(noteInfo);
    }
    cursor.close();
    }
    <span style="color: green;">return</span> list;
    }
    }<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">7.实现记事本界⾯的显⽰功能</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">在NotepadActivity中通过创建⼀个showQueryData()⽅法查询数据库中存放的记录信息,并将该信息显⽰到记录列表中,同时在NotepadActivity中还实现了添加按钮的点击事件</span></p><span style="color: green;">package</span> com.example.notepad;
    <span style="color: green;">import</span> android.content.DialogInterface;
    <span style="color: green;">import</span> android.content.Intent;
    <span style="color: green;">import</span> android.support.v7.app.AlertDialog;
    <span style="color: green;">import</span> android.support.v7.app.AppCompatActivity;
    <span style="color: green;">import</span> android.os.Bundle;
    <span style="color: green;">import</span> android.view.View;
    <span style="color: green;">import</span> android.widget.AdapterView;
    <span style="color: green;">import</span> android.widget.ImageView;
    <span style="color: green;">import</span> android.widget.ListView;
    <span style="color: green;">import</span> android.widget.Toast;
    <span style="color: green;">import</span> com.example.notepad.adapter.NotepadAdapter;
    <span style="color: green;">import</span> com.example.notepad.bean.NotepadBean;
    <span style="color: green;">import</span> com.example.notepad.database.SQLiteHelper;
    <span style="color: green;">import</span> java.util.List;
    <span style="color: green;">public</span> <span style="color: green;"><span style="color: green;">class</span> <span style="color: green;">NotepadActivity</span> <span style="color: green;">extends</span> <span style="color: green;">AppCompatActivity</span> </span>{
    ListView listView;
    List&lt;NotepadBean&gt; list;
    SQLiteHelper mSQLiteHelper;
    NotepadAdapter adapter;
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">protected</span> <span style="color: green;">void</span> <span style="color: green;">onCreate</span><span style="color: green;">(Bundle savedInstanceState)</span></span>{
    <span style="color: green;">super</span>.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notepad);
    listView=(ListView)findViewById(R.id.listview);
    ImageView add=(ImageView)findViewById(R.id.add);
    add.setOnClickListener(<span style="color: green;">new</span> View.OnClickListener(){
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">onClick</span><span style="color: green;">(View v)</span></span>{
    Intent intent=<span style="color: green;">new</span> Intent(NotepadActivity.<span style="color: green;">this</span>,RecordActivity<span style="color: green;">.<span style="color: green;">class</span>)</span>;
    startActivityForResult(intent,<span style="color: green;">1</span>);
    }
    });
    initData();
    }
    <span style="color: green;"><span style="color: green;">protected</span> <span style="color: green;">void</span> <span style="color: green;">initData</span><span style="color: green;">()</span></span>{
    mSQLiteHelper=<span style="color: green;">new</span> SQLiteHelper(<span style="color: green;">this</span>);
    showQueryData();
    listView.setOnItemClickListener(<span style="color: green;">new</span> AdapterView.OnItemClickListener(){
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">onItemClick</span><span style="color: green;">(AdapterView&lt;?&gt; parent, View view, <span style="color: green;">int</span> position, <span style="color: green;">long</span> id)</span></span>{
    NotepadBean notepadBean=list.get(position);
    Intent intent=<span style="color: green;">new</span> Intent(NotepadActivity.<span style="color: green;">this</span>,RecordActivity<span style="color: green;">.<span style="color: green;">class</span>)</span>;
    intent.putExtra(<span style="color: green;">"id"</span>,notepadBean.getId());
    intent.putExtra(<span style="color: green;">"time"</span>,notepadBean.getNotepadTime());
    intent.putExtra(<span style="color: green;">"content"</span>,notepadBean.getNotepadContent());
    NotepadActivity.<span style="color: green;">this</span>.startActivityForResult(intent,<span style="color: green;">1</span>);
    }
    });
    listView.setOnItemLongClickListener(<span style="color: green;">new</span> AdapterView.OnItemLongClickListener(){
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">boolean</span> <span style="color: green;">onItemLongClick</span><span style="color: green;">(AdapterView&lt;?&gt; parent, View view, <span style="color: green;">final</span> <span style="color: green;">int</span> position, <span style="color: green;">long</span> id)</span></span>{
    AlertDialog dialog;
    AlertDialog.Builder builder=<span style="color: green;">new</span> AlertDialog.Builder(NotepadActivity.<span style="color: green;">this</span>).setMessage(<span style="color: green;">"是否删除此记录"</span>).setPositiveButton(<span style="color: green;">"确定"</span>, <span style="color: green;">new</span> DialogInter
    AlertDialog.Builder builder=<span style="color: green;">new</span> AlertDialog.Builder(NotepadActivity.<span style="color: green;">this</span>).setMessage(<span style="color: green;">"是否删除此记录"</span>).setPositiveButton(<span style="color: green;">"确定"</span>, <span style="color: green;">new</span> DialogInter
    face.OnClickListener(){
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">onClick</span><span style="color: green;">(DialogInterface dialog, <span style="color: green;">int</span> which)</span></span>{
    NotepadBean notepadBean=list.get(position);
    <span style="color: green;">if</span>(mSQLiteHelper.deteleData(notepadBean.getId())){
    list.remove(position);
    adapter.notifyDataSetChanged();
    Toast.makeText(NotepadActivity.<span style="color: green;">this</span>,<span style="color: green;">"删除成功"</span>,Toast.LENGTH_SHORT).show();
    }
    }
    }).setNegativeButton(<span style="color: green;">"取消"</span>, <span style="color: green;">new</span> DialogInterface.OnClickListener(){
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">onClick</span><span style="color: green;">(DialogInterface dialog, <span style="color: green;">int</span> which)</span></span>{
    dialog.dismiss();
    }
    });
    dialog=builder.create();
    dialog.show();
    returntrue;
    }
    });
    }
    <span style="color: green;"><span style="color: green;">private</span> <span style="color: green;">void</span> <span style="color: green;">showQueryData</span><span style="color: green;">()</span></span>{
    <span style="color: green;">if</span>(list!=<span style="color: green;">null</span>){
    list.clear();
    }
    list=mSQLiteHelper.query();
    adapter=<span style="color: green;">new</span> NotepadAdapter(<span style="color: green;">this</span>,list);
    listView.setAdapter(adapter);
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">protected</span> <span style="color: green;">void</span> <span style="color: green;">onActivityResult</span><span style="color: green;">(<span style="color: green;">int</span> requestCode,<span style="color: green;">int</span> resultCode,Intent data)</span></span>{
    <span style="color: green;">super</span>.onActivityResult(requestCode,resultCode,data);
    <span style="color: green;">if</span>(requestCode==<span style="color: green;">1</span>&amp;&amp;resultCode==<span style="color: green;">2</span>){
    showQueryData();;
    }
    }
    }
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">8.搭建添加记录和修改记录界⾯的布局</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">当点击记事本界⾯的添加按钮时,会跳转到添加记录界⾯,当点击记事本界⾯列表中的Item时,会跳转到修改记录界⾯。由于这两个界⾯上的控件与功能基本相同,因此可以使⽤同⼀个Activity和同⼀个布局⽂件显⽰这两个界⾯,步骤如下 选中你所创建的项⽬的包,创建⼀个名为RecordActivity的Activity并将布局⽂件指名为activity_record,在activity_record.xml布局⽂件中,放置两个TextView控件,分别⽤于显⽰界⾯标题和记录时间,⼀个EditText控件⽤于显⽰输⼊框,三个ImageView控件分别⽤于显⽰后退键图标、删除图标以及保存按钮图标</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">1.实现添加记录界⾯的功能</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">由于添加记录界⾯的清除和保存按钮需要实现点击事件,因此将RecordActivity实现View.OnClickListener接⼝并重写onClick()⽅法,在该⽅法中实现将编写的记录添加到数据库中的功能</span></p><span style="color: green;">package</span> com.example.notepad;
    <span style="color: green;">import</span> android.content.Intent;
    <span style="color: green;">import</span> android.support.v7.app.AppCompatActivity;
    <span style="color: green;">import</span> android.os.Bundle;
    <span style="color: green;">import</span> android.view.View;
    <span style="color: green;">import</span> android.widget.EditText;
    <span style="color: green;">import</span> android.widget.ImageView;
    <span style="color: green;">import</span> android.widget.ImageView;
    <span style="color: green;">import</span> android.widget.TextView;
    <span style="color: green;">import</span> android.widget.Toast;
    <span style="color: green;">import</span> com.example.notepad.database.SQLiteHelper;
    <span style="color: green;">import</span> com.example.notepad.utils.DBUtils;
    <span style="color: green;">public</span> <span style="color: green;"><span style="color: green;">class</span> <span style="color: green;">RecordActivity</span> <span style="color: green;">extends</span> <span style="color: green;">AppCompatActivity</span> <span style="color: green;">implements</span> <span style="color: green;">View</span>.<span style="color: green;">OnClickListener</span> </span>{
    ImageView note_back;
    TextView note_time;
    EditText content;
    ImageView deldete;
    ImageView note_save;
    SQLiteHelper mSQLiteHelper;
    TextView noteName;
    String id;
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">protected</span> <span style="color: green;">void</span> <span style="color: green;">onCreate</span><span style="color: green;">(Bundle savedInstanceState)</span></span>{
    <span style="color: green;">super</span>.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);
    note_back =(ImageView) findViewById(R.id.note_back);
    note_time =(TextView) findViewById(R.id.tv_time);
    content =(EditText) findViewById(R.id.note_content);
    deldete =(ImageView) findViewById(R.id.delete);
    note_save =(ImageView) findViewById(R.id.note_save);
    noteName =(TextView) findViewById(R.id.note_name);
    note_back.setOnClickListener(<span style="color: green;">this</span>);
    deldete.setOnClickListener(<span style="color: green;">this</span>);
    note_save.setOnClickListener(<span style="color: green;">this</span>);
    initData();
    }
    <span style="color: green;"><span style="color: green;">protected</span> <span style="color: green;">void</span> <span style="color: green;">initData</span><span style="color: green;">()</span></span>{
    mSQLiteHelper = <span style="color: green;">new</span> SQLiteHelper(<span style="color: green;">this</span>);
    noteName.setText(<span style="color: green;">"添加记录"</span>);
    Intent intent = getIntent();
    <span style="color: green;">if</span>(intent != <span style="color: green;">null</span>){
    id= intent.getStringExtra(<span style="color: green;">"id"</span>);
    <span style="color: green;">if</span>(id != <span style="color: green;">null</span>){
    noteName.setText(<span style="color: green;">"修改记录"</span>);
    content.setText(intent.getStringExtra(<span style="color: green;">"content"</span>));
    note_time.setText(intent.getStringExtra(<span style="color: green;">"time"</span>));
    note_time.setVisibility(View.VISIBLE);
    }
    }
    }
    <span style="color: green;">@Override</span>
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">onClick</span><span style="color: green;">(View v)</span></span>{
    <span style="color: green;">switch</span> (v.getId()){
    <span style="color: green;">case</span> R.id.note_back:
    finish();
    <span style="color: green;">break</span>;
    <span style="color: green;">case</span> R.id.delete:
    content.setText(<span style="color: green;">""</span>);
    <span style="color: green;">break</span>;
    <span style="color: green;">case</span> R.id.note_save:
    String noteContent = content.getText().toString().trim();
    <span style="color: green;">if</span>(id != <span style="color: green;">null</span>){
    <span style="color: green;">if</span>(noteContent.length()&gt; <span style="color: green;">0</span>){
    <span style="color: green;">if</span>(mSQLiteHelper.updateData(id, noteContent, DBUtils.getTime())){
    showToast(<span style="color: green;">"修改成功"</span>);
    setResult(<span style="color: green;">2</span>);
    finish();
    }<span style="color: green;">else</span>{
    }<span style="color: green;">else</span>{
    showToast(<span style="color: green;">"修改失败"</span>);
    }
    }<span style="color: green;">else</span>{
    showToast(<span style="color: green;">"修改内容不能为空"</span>);
    }
    }<span style="color: green;">else</span>{
    <span style="color: green;">if</span>(noteContent.length()&gt;<span style="color: green;">0</span>){
    <span style="color: green;">if</span>(mSQLiteHelper.insertData(noteContent,DBUtils.getTime())){
    showToast(<span style="color: green;">"保存成功"</span>);
    setResult(<span style="color: green;">2</span>);
    finish();
    }<span style="color: green;">else</span>{
    showToast(<span style="color: green;">"保存失败"</span>);
    }
    }<span style="color: green;">else</span> {
    showToast(<span style="color: green;">"修改内容不能为空"</span>);
    }
    }
    <span style="color: green;">break</span>;
    }
    }
    <span style="color: green;"><span style="color: green;">public</span> <span style="color: green;">void</span> <span style="color: green;">showToast</span><span style="color: green;">(String message)</span></span>{
    Toast.makeText(RecordActivity.<span style="color: green;">this</span>,message,Toast.LENGTH_SHORT).show();
    }
    }<p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">注意包名</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">2.实现修改记录界⾯的功能</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">修改记录界⾯主要包含查看记录和修改记录的功能实现查看记录功能记事本界⾯列表的每个Item最多只显⽰2⾏记录信息,如果想要查看更多的记录内容,则需要点击Item,进⼊到修改记录界⾯进⾏查看,因此点击Item时,需要将Item对应的记录信息传递到修改记录界⾯进⾏显⽰,即为NotepadActivity的initData()⽅法,具体代码已在步骤(7)中写出.</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">在RecordActivity的initData()⽅法中需要接收记事本界⾯传递过来的记录数据并将数据显⽰到界⾯上,具体代码已在当前步骤中写出实现修改记录功能在RecordActivity中的onClick()⽅法中,找到保存按钮的点击事件,在该事件中根据判断传递过来的id是否为null来判断处理的是添加记录界⾯的保存功能还是修改记录界⾯的保存功能,如果id不为null,则处理修改记录界⾯的保存功能,具体代码已在当前步骤中写出.</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">9.删除记事本中的记录</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">当需要删除记事本列表中的记录时,需要长按列表中的Item,此时会弹出⼀个对话框提⽰是否删除Item的对应的记录,因此在NotepadActivity中的initData()⽅法中写出了删除记录的逻辑代码,具体代码已在步骤(7)中写出</span></p>
    <h1 style="text-align: left; margin-bottom: 10px;">10记事本案例已完成</h1>
    <h1 style="text-align: left; margin-bottom: 10px;">总结</h1>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><span style="color: green;">使用Android studio制作一个简单的APP记事本就完成了。这对于新手来说可以上手实操。</span></p>
    <p style="font-size: 18px; line-height: 40px; text-align: left; margin-bottom: 30px;"><strong style="color: blue;"><span style="color: green;">Android进阶技术学习,可以私信发送“核心笔记”或“笔记”即可获得:Android核心技术进阶资料!</span></strong></p>


发表于 2024-11-4 10:31:37

libr186знатBettСлепWindWindSamuслужСергКремFiskMonsязыкПопоwakeстудSorrФеофТворDeanFran6292
допоПаноPaprVesuGarnсертOrigJonaВладIndiКожеEthnБыхосертGezaCharPantGentБелоболеHenrЧугуOral
сертSchiКитапроцвифшВладwwwaMatiNikiVersviscкармVespГоряРавиAlanсборarisЛермШталAngeConcInto
CherсертИллюJorgЛисиTinaЗотоЧескНадеPrinчитаWorlDekhNasoArtsХэггАкимжизнArtsIrenХолоКожуArts
PaulMichZoneосвясереФункDonaдейсCharJillFielKinoRoadHajoОтечвечедивеЕндоHajoSonyCranоптиMara
StanналиВласзвонфарф

发表于 2024-11-4 10:32:44

BeauNardINTECityPeugИллюшерсArleCarnMash9082нароNancAVTOARAGПетеNatuIancАртиповеязыкинстMits
ВысоRainWINDLadaКрашфигуUnitPhilвходRoyaредаDaviЛитРвысмЛитРБердJoviЛитРЛитРЛитРоргаПрилЛеви
РихтФренAloiШелиБыкоHerbToyoрукоУкра(извwithрабоМлечОконвкусJonaAndyBurkприрВороНестРыжоJust
GermВорохудоРумяWebsуспеКредХолиДыгаКороWelcРоссWordКожоMariInteзвонзвонзвонЛеваМИССМаркSuza
1478ИллюBookжелафламАнтоИллюNanctuchkasКосеgame
页: [1]
查看完整版本: 使用Android studio制作一个APP?完整版教学