简易的记事本
MainActivity
package com.example.note20200608;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
//可优化的地方:用字符输出流。用同一的捕获异常,用share,
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final String FILE_NAME = "memo";
private ImageView Iv_bc;
private ImageView Iv_qk;
private ImageView Iv_ld;
byte[] buffer = null;
private long exitTime = 0;
private TextToSpeech tts;//定义tts对象
private String textView ;//定义文本对象,用于显示要朗读的文本
EditText editText ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//——————
editText =(EditText)findViewById(R.id.editText);
tts =new TextToSpeech(this,this);//初始化TTS对象
Iv_ld =(ImageView) super.findViewById(R.id.ld);//定义按钮对象并获取
Iv_ld.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView=editText.getText().toString();
tts.speak(textView, TextToSpeech.QUEUE_FLUSH, null);//开始朗读,设置朗读的参数,第一个是播放源,第二参数是播放的模式
}
});
//——————————————————————————————
// TextView etext = (TextView)findViewById(R.id.textView);
Iv_bc = (ImageView) findViewById(R.id.bc);
Iv_qk = (ImageView) findViewById(R.id.qk);
Iv_bc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos =null;//声明文件输出流
BufferedOutputStream bos = new BufferedOutputStream(fos);//把低级的字节输出流包装成一个高级的缓冲字节输出流
String text = editText.getText().toString();//获取输入的备忘信息
try {
bos = new BufferedOutputStream(openFileOutput(FILE_NAME,MODE_PRIVATE));
//bos = openFileOutput()
// fos = openFileOutput("memo",MODE_PRIVATE);
bos.write(text.getBytes());//保存备忘信息
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
/*读取保存的备忘信息*/
FileInputStream fis = null;//声明文件输入流对象
BufferedInputStream bis = new BufferedInputStream(fis);
try {
//bos = new BufferedOutputStream(openFileOutput("meno",MODE_PRIVATE));
bis = new BufferedInputStream(openFileInput(FILE_NAME));
// bis = new BufferedInputStream(openFileOutput("memo"),MODE_PRIVATE);
//bis=openFileInput("meno");
//fis=openFileInput("memo");//获取文件输入流对象
buffer=new byte[bis.available()];
bis.read(buffer);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bis != null) {
try {
String data = new String(buffer);//把字符数组中的数据转换为字符串
editText.setText(data);//显示读取的内容
bis.close();//关闭输入对象
} catch (Exception e) {
e.printStackTrace();
}
}
}
Iv_qk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
}
//
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);//getMenuInflater()方法得到MenuInflater
return super.onCreateOptionsMenu(menu);
}
//
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//创建菜单项的点击事件
switch (item.getItemId()) {
case R.id.gy:
Intent intent = new Intent(MainActivity.this,introduce.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/* 复制到剪切板*/
public void onClickCopy(View v) {
// 从API11开始android推荐使用android.content.ClipboardManager
// 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager,虽然提示deprecated,但不影响使用。
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 将文本内容放到系统剪贴板里。
cm.setText(editText.getText().toString());
Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
}
/*双击退出*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
public void exit() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
}
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {//如果状态成功的话
int result = tts.setLanguage(Locale.CHINA);//设置语音为中文
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {//如果朗读的数据不存在或是不支持
Toast.makeText(this,"抱歉,不能进行朗读",Toast.LENGTH_LONG).show();//弹出消息提示框进行提示
}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.note20200608">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAG" />
<application
android:allowBackup="true"
android:icon="@drawable/tubiao"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".introduce"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
String.xml
<resources>
<string name="app_name">备忘录</string>
<string name="bc">保存</string>
<string name="qk">清空</string>
<string name="ld">朗读</string>
<string name="jqb">复制</string>
<string name="editText">请输入文字</string>
<string name="gy">关于</string>
<string name="introduce"></string>
<string name="vesion">版本:1.0.1</string>
<string name="textview">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t版本信息:\n
\t\t 1.0.1:优化存读数据的速度
</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<EditText
android:id="@+id/editText"
android:layout_width="416dp"
android:layout_height="418dp"
android:layout_marginBottom="12dp"
android:autofillHints=""
android:ems="10"
android:gravity="top"
android:hint="@string/editText"
android:inputType="textMultiLine"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.84" />
<ImageView
android:id="@+id/bc"
android:layout_width="146dp"
android:layout_height="55dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:contentDescription="@string/bc"
android:src="@drawable/button"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<ImageView
android:id="@+id/ld"
android:layout_width="147dp"
android:layout_height="55dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="44dp"
android:contentDescription="@string/ld"
android:src="@drawable/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline4" />
<ImageView
android:id="@+id/qk"
android:layout_width="146dp"
android:layout_height="55dp"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="28dp"
android:contentDescription="@string/qk"
android:src="@drawable/button"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:layout_marginBottom="64dp"
android:text="@string/ld"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline4" />
<ImageView
android:id="@+id/jqb"
android:layout_width="146dp"
android:layout_height="55dp"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginBottom="44dp"
android:contentDescription="@string/jqb"
android:onClick="onClickCopy"
android:src="@drawable/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:text="@string/bc"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginLeft="84dp"
android:layout_marginBottom="64dp"
android:text="@string/jqb"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="84dp"
android:layout_marginLeft="84dp"
android:layout_marginTop="44dp"
android:text="@string/qk"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="50dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_introduce.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".introduce">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="@string/introduce"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginLeft="108dp"
android:layout_marginBottom="40dp"
android:text="@string/vesion"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="411dp"
android:layout_height="321dp"
android:layout_marginTop="136dp"
android:text="@string/textview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
</androidx.constraintlayout.widget.ConstraintLayout>
源码下载链接
链接:https://pan.baidu.com/s/1iV5F8SdGKebT9AZ2FTTjRQ
提取码:dgwv