android备忘录教学_Android Studio-备忘录功能实现
终于,Android作业弄完了,最后一个,备忘录教学。
相关安卓教学内容:
首先第一步,还是老样子,创建一个NoteActivity。
image.png
第二步,打开activity_note.xml,开始布局,话不多说了,关于这一块的内容我在登录,注册当中已经教学的很详细了,直接上代码吧,反正我码再多字估计你们也不看....
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"
tools:context=".NoteActivity"
android:background="@drawable/notebg">
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="备忘录"
android:textSize="30dp"/>
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:minLines="17"
android:inputType="textMultiLine"
android:hint="点击此处输入文字"
android:background="@null"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentStart="true"
android:layout_above="@+id/button4"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView3"
android:gravity="left|top"
android:textSize="20dp"/>
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/editText3" />
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/editText3" />
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignEnd="@+id/editText3"
android:text="0个字" />
效果如下:怎么样,看上去还不错吧?
image.png
接下来打开NoteActivity,直接上代码,不想码注释了,码了也没人看,反正你们最喜欢的就是复制粘贴代码
package com.wxy.homework;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class NoteActivity extends AppCompatActivity {
private EditText inputInfo;
private Button save;
private Button reset;
private TextView count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setFullScreen();
hideBar();
inputInfo = (EditText) findViewById(R.id.editText3);
save = (Button) findViewById(R.id.button4);
reset = (Button) findViewById(R.id.button5);
count = (TextView)findViewById(R.id.textView4);
inputInfo.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
count.setText(inputInfo.getText().length()+"个字");
}
});
onload();
inputInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputInfo.setCursorVisible(true);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileOutputStream fos = null;
try{
fos = openFileOutput("txt", Context.MODE_PRIVATE);
String text = inputInfo.getText().toString();
fos.write(text.getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(fos!=null){
fos.flush();
Toast.makeText(NoteActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();
fos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileOutputStream fos = null;
inputInfo.setText("");
try{
fos = openFileOutput("txt", Context.MODE_PRIVATE);
String text = "";
fos.write(text.getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if(fos!=null){
fos.flush();
Toast.makeText(NoteActivity.this,"清空成功!",Toast.LENGTH_SHORT).show();
fos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
});
}
public void onload(){
FileInputStream fis = null;
try{
fis = openFileInput("txt");
if(fis.available()==0){
return;
}else{
byte[] con = new byte[fis.available()];
while(fis.read(con)!=-1){
}
inputInfo.setText(new String(con));
inputInfo.setSelection(inputInfo.getText().length());
inputInfo.setCursorVisible(false);
}
}catch(Exception e){
e.printStackTrace();
}
}
long time;
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
if(System.currentTimeMillis()-time>2000){
Toast.makeText(NoteActivity.this,"再次点击返回键,程序退出",Toast.LENGTH_SHORT).show();
time = System.currentTimeMillis();
}else{
NoteActivity.this.finish();
}
return true;
}
return super.onKeyDown(keyCode,event);
}
private void hideBar(){
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null){
actionBar.hide();
}
}
private void setFullScreen(){
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
然后,老师作业要求是,登录之后,直接跳转到备忘录,所以我们要调整启动顺序。打开LoginActivity,
调整启动顺序
image.png
好了,激动人心的时候又到了,直接开始测试
我们输入之前注册的用户名称和密码进行登录
image.png
发现登录成功,完美跳转到备忘录界面
image.png
我们输入任意字符,点击保存,发现保存成功,且下次登录时,直接显示保存的字符
image.png
我们点击右下角的重置,发现备忘录内容全部清空,完美运行
image.png
好了我亲爱的同学们,安卓作业搞定了。
欢迎关注我,我将不定期更新教学博客和技术贴。
android备忘录教学_Android Studio-备忘录功能实现相关推荐
- android备忘录教学_Android Studio实战 - 备忘录实验
[导读]到目前为止,你已经熟悉了关于创建新项目.编程和重构的基础知识,是时候创建一款 Android应用(也称为App) 了. 到目前为止,你已经熟悉了关于创建新项目.编程和重构的基础知识,是时候创建 ...
- android备忘录教学_Android学习备忘录
看的框架层相关的概念容易忘记,写个备忘录把理解的重点和遇到的疑惑记下. Context:用Android最早接触到的一个类,使用非常非常广泛,在各个地方都要用到,像Toast.Dialog.new T ...
- android备忘录教学_android基础备忘录(一)
1.drawable-(hdpi,mdpi,ldpi)的区别 dpi是"dot per inch"的缩写,每英寸像素数. 四种密度分类: ldpi (low), mdpi (med ...
- android备忘录教学_android备忘录教学_Android Studio-备忘录功能实现
终于,Android作业弄完了,最后一个,备忘录教学. 相关安卓教学内容: 首先第一步,还是老样子,创建一个NoteActivity. image.png 第二步,打开activity_note.xm ...
- android tag 快捷_Android Studio快捷键生成TAG、Log.x日志输出介绍
生成TAG logt+Tab键: private static final String TAG = "Extract"; 生成Log.d() logd+Tab键: Log.d(T ...
- android shn1 获取_Android Studio获取开发版SHA1值和发布版SHA1值的史上最详细方法
前言:使用百度地图时需要秘钥,申请秘钥时需要SHA1值,所以今天就总结一下怎么获取这个值. 正常情况下: 一.获取开发版SHA1: 在此我直接用AndroidStudio提供的命令控制台了,毕竟做An ...
- android tag 快捷_Android Studio快捷键使用
logt 生成 TAG标签 private static final String TAG = "MainActivity"; logm 打印方法具体日志 Log.d(TAG, & ...
- android java显示_Android Studio没有显示java类源代码
我搜索了这个问题并尝试了所提出的解决方案但没有成功.我有一个项目,当我去查看源文件时,让我说Fragment它让我到 java类就好了. 现在当我去File – >新项目并完成所有相同的设置程序 ...
- Android设计模式详解之备忘录模式
前言 备忘录模式是一种行为模式,该模式用于保存对象当前状态,并且在之后可以再次恢复到此状态: 定义:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样,以后就可以将该对象恢 ...
最新文章
- nginx+php-fpm 的配置下,php的错误日志
- 实现锁死的有滚动条的div的表格(datagird)
- 推荐系统炼丹笔记:RecSys2020-SSE-PT解锁序列数据挖掘新姿势
- debian安装搜狗
- html 甘特图_Rplotly|交互式甘特图(Gantt chart)项目管理/学习计划
- 在Windows下编译ffmpeg完全手册
- arduinojson 转 string_安德胜工作室发来本周五嗨唱转起来第二季首秀的嘉宾剧透...
- xy坐标正负方向_【笛卡尔坐标系】
- poj1113/hdu1348(凸包。。。两个网站上的输入输出有点出入)
- 微课|中学生可以这样学Python(例5.4):计算决赛现场选手得分
- html dom手机版,HTML DOM - 元素
- python怎么开始_Python自己学习怎么开始?
- PouchContainer 开源版本及内部版本一致性实践
- 9.4SAS软件入门
- 脚本重启电信天翼网关
- 如何管理计算机回收站,回收站功能多多 教你如何玩转回收站
- AngularJs:Directive指令用法
- Python图算法之狄克斯特拉算法
- android9 0正式版,安卓9.0系统正式版|Android Pie 9.0 正式版 - 天天游戏吧
- 【basepro】常用util