Android Studio-備忘錄功能實(shí)現(xiàn)

終于,Android作業(yè)弄完了辕近,最后一個(gè)翻斟,備忘錄教學(xué)曲聂。

相關(guān)安卓教學(xué)內(nèi)容:

啟動界面http://www.reibang.com/p/7e0955291b18
登錄注冊功能http://www.reibang.com/p/3882eaf8693e

首先第一步,還是老樣子柔滔,創(chuàng)建一個(gè)NoteActivity溢陪。

image.png

第二步,打開activity_note.xml廊遍,開始布局嬉愧,話不多說了,關(guān)于這一塊的內(nèi)容我在登錄喉前,注冊當(dāng)中已經(jīng)教學(xué)的很詳細(xì)了,直接上代碼吧王财,反正我碼再多字估計(jì)你們也不看....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".NoteActivity"
    android:background="@drawable/notebg">
    <TextView
        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"/>

    <EditText
        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="點(diǎn)擊此處輸入文字"
        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"/>

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

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

    <TextView
        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個(gè)字" />

</RelativeLayout>

效果如下:怎么樣卵迂,看上去還不錯(cuò)吧?

image.png

接下來打開NoteActivity,直接上代碼,不想碼注釋了绒净,碼了也沒人看见咒,反正你們最喜歡的就是復(fù)制粘貼代碼

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()+"個(gè)字");
            }
        });

        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,"再次點(diǎn)擊返回鍵改览,程序退出",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);
    }

}

然后下翎,老師作業(yè)要求是,登錄之后宝当,直接跳轉(zhuǎn)到備忘錄视事,所以我們要調(diào)整啟動順序。打開LoginActivity,
調(diào)整啟動順序

image.png

好了庆揩,激動人心的時(shí)候又到了俐东,直接開始測試

我們輸入之前注冊的用戶名稱和密碼進(jìn)行登錄

image.png

發(fā)現(xiàn)登錄成功,完美跳轉(zhuǎn)到備忘錄界面

image.png

我們輸入任意字符订晌,點(diǎn)擊保存虏辫,發(fā)現(xiàn)保存成功,且下次登錄時(shí)锈拨,直接顯示保存的字符

image.png

我們點(diǎn)擊右下角的重置砌庄,發(fā)現(xiàn)備忘錄內(nèi)容全部清空,完美運(yùn)行

image.png

好了我親愛的同學(xué)們奕枢,安卓作業(yè)搞定了鹤耍。
歡迎關(guān)注我,我將不定期更新教學(xué)博客和技術(shù)貼验辞。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末稿黄,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子跌造,更是在濱河造成了極大的恐慌杆怕,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件壳贪,死亡現(xiàn)場離奇詭異陵珍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)违施,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門互纯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人磕蒲,你說我怎么就攤上這事留潦。” “怎么了辣往?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵兔院,是天一觀的道長。 經(jīng)常有香客問我站削,道長坊萝,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮十偶,結(jié)果婚禮上菩鲜,老公的妹妹穿的比我還像新娘。我一直安慰自己惦积,他們只是感情好接校,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著荣刑,像睡著了一般馅笙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上厉亏,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天董习,我揣著相機(jī)與錄音,去河邊找鬼爱只。 笑死皿淋,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的恬试。 我是一名探鬼主播窝趣,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼训柴!你這毒婦竟也來了哑舒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤幻馁,失蹤者是張志新(化名)和其女友劉穎洗鸵,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仗嗦,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡膘滨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了稀拐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片火邓。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖德撬,靈堂內(nèi)的尸體忽然破棺而出铲咨,到底是詐尸還是另有隱情,我是刑警寧澤砰逻,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布鸣驱,位于F島的核電站,受9級特大地震影響蝠咆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一刚操、第九天 我趴在偏房一處隱蔽的房頂上張望闸翅。 院中可真熱鬧,春花似錦菊霜、人聲如沸坚冀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽记某。三九已至,卻和暖如春构捡,著一層夾襖步出監(jiān)牢的瞬間液南,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工勾徽, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滑凉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓喘帚,卻偏偏與公主長得像畅姊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子吹由,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容