android写简单记事本
In this tutorial I have shared a very simple notepad app android example. I have used file handling to read and write data. The notepad consists of three options to create new file, save and open a file. When you click on save or open button an alert dialog opens where you have to enter the file name. Internal memory is used to save the text file. At the end I have shared a link to download the apk for this example so that you can directly install and run it in your phone.
在本教程中,我分享了一个非常简单的记事本应用android示例。 我已经使用文件处理来读取和写入数据。 记事本包含三个选项,用于创建新文件,保存和打开文件。 单击“保存”或“打开”按钮时,将打开一个警告对话框,您必须在其中输入文件名。 内部存储器用于保存文本文件。 最后,我分享了一个下载该示例apk的链接,以便您可以直接在手机中安装和运行它。
Also Read: How to Make a Calculator App for Android
另请阅读: 如何制作适用于Android的计算器应用
简单记事本应用Android示例 (Simple Notepad App Android Example)
MainActivity.java
MainActivity.java
It contains the actual source code responsible for all the working.
它包含负责所有工作的实际源代码。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package com.thecrazyprogrammer.www.notepadapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button newButton,saveButton,openButton;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newButton=(Button)findViewById(R.id.newButton);
saveButton=(Button)findViewById(R.id.saveButton);
openButton=(Button)findViewById(R.id.openButton);
text=(EditText)findViewById(R.id.text);
}
public void buttonAction(View v) {
final EditText fileName=new EditText(this);
AlertDialog.Builder ad=new AlertDialog.Builder(this);
ad.setView(fileName);
if (v.getId() == R.id.saveButton) {
ad.setMessage("Save File");
ad.setPositiveButton("Save",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
FileOutputStream fout=openFileOutput(fileName.getText().toString()+".txt",MODE_WORLD_READABLE);
fout.write(text.getText().toString().getBytes());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error Occured: "+e,Toast.LENGTH_LONG).show();
}
}
});
ad.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
ad.show();
}
if(v.getId()==R.id.openButton) {
ad.setMessage("Open File");
ad.setPositiveButton("Open",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int c;
text.setText("");
try {
FileInputStream fin = openFileInput(fileName.getText().toString()+".txt");
while ((c = fin.read()) != -1)
{
text.setText((text.getText().toString() + Character.toString((char) c)));
}
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error Occured: "+e,Toast.LENGTH_LONG).show();
}
}
});
ad.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
ad.show();
}
if(v.getId()==R.id.newButton) {
text.setText("");
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package com . thecrazyprogrammer . www . notepadapp ;
import android . app . Activity ;
import android . app . AlertDialog ;
import android . content . DialogInterface ;
import android . os . Bundle ;
import android . view . View ;
import android . widget . Button ;
import android . widget . EditText ;
import android . widget . Toast ;
import java . io . FileInputStream ;
import java . io . FileOutputStream ;
public class MainActivity extends Activity {
Button newButton , saveButton , openButton ;
EditText text ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
newButton = ( Button ) findViewById ( R . id . newButton ) ;
saveButton = ( Button ) findViewById ( R . id . saveButton ) ;
openButton = ( Button ) findViewById ( R . id . openButton ) ;
text = ( EditText ) findViewById ( R . id . text ) ;
}
public void buttonAction ( View v ) {
final EditText fileName = new EditText ( this ) ;
AlertDialog . Builder ad = new AlertDialog . Builder ( this ) ;
ad . setView ( fileName ) ;
if ( v . getId ( ) == R . id . saveButton ) {
ad . setMessage ( "Save File" ) ;
ad . setPositiveButton ( "Save" , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
try {
FileOutputStream fout = openFileOutput ( fileName . getText ( ) . toString ( ) + ".txt" , MODE_WORLD_READABLE ) ;
fout . write ( text . getText ( ) . toString ( ) . getBytes ( ) ) ;
} catch ( Exception e ) {
Toast . makeText ( getApplicationContext ( ) , "Error Occured: " + e , Toast . LENGTH_LONG ) . show ( ) ;
}
}
} ) ;
ad . setNegativeButton ( "Cancel" , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
dialog . cancel ( ) ;
}
} ) ;
ad . show ( ) ;
}
if ( v . getId ( ) == R . id . openButton ) {
ad . setMessage ( "Open File" ) ;
ad . setPositiveButton ( "Open" , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
int c ;
text . setText ( "" ) ;
try {
FileInputStream fin = openFileInput ( fileName . getText ( ) . toString ( ) + ".txt" ) ;
while ( ( c = fin . read ( ) ) != - 1 )
{
text . setText ( ( text . getText ( ) . toString ( ) + Character . toString ( ( char ) c ) ) ) ;
}
} catch ( Exception e ) {
Toast . makeText ( getApplicationContext ( ) , "Error Occured: " + e , Toast . LENGTH_LONG ) . show ( ) ;
}
}
} ) ;
ad . setNegativeButton ( "Cancel" , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
dialog . cancel ( ) ;
}
} ) ;
ad . show ( ) ;
}
if ( v . getId ( ) == R . id . newButton ) {
text . setText ( "" ) ;
}
}
}
|
activity_main.xml
activity_main.xml
This xml file contains the code for the design of notepad app.
该xml文件包含用于记事本应用程序设计的代码。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.com.thecrazyprogrammer.www.notepadapp"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:gravity="top"
android:id="@+id/text"
android:scrollbars = "vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New"
android:id="@+id/newButton"
android:layout_weight="1"
android:onClick="buttonAction"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/saveButton"
android:onClick="buttonAction"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open"
android:id="@+id/openButton"
android:onClick="buttonAction"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
< LinearLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns : tools = "http://schemas.android.com/tools" android : layout_width = "match_parent"
android : layout_height = "match_parent" android : paddingLeft = "@dimen/activity_horizontal_margin"
android : paddingRight = "@dimen/activity_horizontal_margin"
android : paddingTop = "@dimen/activity_vertical_margin"
android : paddingBottom = "@dimen/activity_vertical_margin"
tools : context = "com.com.thecrazyprogrammer.www.notepadapp"
android : orientation = "vertical"
android : weightSum = "10" >
< LinearLayout
android : layout_width = "match_parent"
android : layout_height = "0dp"
android : layout_weight = "9"
android : orientation = "vertical" >
< EditText
android : layout_width = "match_parent"
android : layout_height = "match_parent"
android : inputType = "textMultiLine"
android : gravity = "top"
android : id = "@+id/text"
android : scrollbars = "vertical" / >
< / LinearLayout >
< LinearLayout
android : layout_width = "match_parent"
android : layout_height = "0dp"
android : layout_weight = "1" >
< Button
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : text = "New"
android : id = "@+id/newButton"
android : layout_weight = "1"
android : onClick = "buttonAction" / >
< Button
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : text = "Save"
android : id = "@+id/saveButton"
android : onClick = "buttonAction"
android : layout_weight = "1" / >
< Button
android : layout_width = "match_parent"
android : layout_height = "wrap_content"
android : text = "Open"
android : id = "@+id/openButton"
android : onClick = "buttonAction"
android : layout_weight = "1" / >
< / LinearLayout >
< / LinearLayout >
|
If you have any doubts regarding above notepad android example then comment below.
如果您对上面的记事本android示例有任何疑问,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2015/08/simple-notepad-app-android-example.html
android写简单记事本