終于,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溢陪。
第二步,打開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ò)吧?
接下來打開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)整啟動順序
好了庆揩,激動人心的時(shí)候又到了俐东,直接開始測試
我們輸入之前注冊的用戶名稱和密碼進(jìn)行登錄
發(fā)現(xiàn)登錄成功,完美跳轉(zhuǎn)到備忘錄界面
我們輸入任意字符订晌,點(diǎn)擊保存虏辫,發(fā)現(xiàn)保存成功,且下次登錄時(shí)锈拨,直接顯示保存的字符
我們點(diǎn)擊右下角的重置砌庄,發(fā)現(xiàn)備忘錄內(nèi)容全部清空,完美運(yùn)行
好了我親愛的同學(xué)們奕枢,安卓作業(yè)搞定了鹤耍。
歡迎關(guān)注我,我將不定期更新教學(xué)博客和技術(shù)貼验辞。