android备忘录教学_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备忘录教学_Android Studio-备忘录功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZUCC_计算机网络实验_实验01 实验
- 下一篇: 怎样免费下载知网、万方数据库等文献库的文