作者:手机用户2502887415 | 来源:互联网 | 2024-11-30 11:14
首先,需要在项目的AndroidManifest.xml
文件中添加对外部存储的写入权限,确保应用能够访问并写入外部存储设备。
1
2
3 4 android:allowBackup="true"
5 android:icon="@drawable/ic_launcher"
6 android:label="@string/app_name"
7 android:theme="@style/AppTheme" >
8 9 android:name="com.example.androidfilestream04.MainActivity"
10 android:label="@string/app_name" >
11
12
13
14
15
16
接下来,设计用户界面,通过TableLayout
布局来创建一个简单的表单,包含姓名和邮箱的输入框以及一个提交按钮。
1 2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/TableLayout1"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent" >
6
7
8
9 10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:text="姓名" />
13
14 15 android:id="@+id/name"
16 android:layout_width="match_parent"
17 android:layout_height="wrap_content" />
18
19
20
21
22 23 android:layout_width="wrap_content"
24 android:layout_height="wrap_content"
25 android:text="邮箱" />
26
27 28 android:id="@+id/email"
29 android:layout_width="match_parent"
30 android:layout_height="wrap_content" />
31
32
33
34
35
41
最后,在MainActivity
中实现逻辑处理,当用户点击提交按钮时,应用会检查外部存储是否可用,并将用户输入的信息以XML格式保存到指定的文件中。
1 package com.example.androidfilestream04;
2
3 import java.io.File;
4 import java.io.StringWriter;
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7 import javax.xml.parsers.ParserConfigurationException;
8 import javax.xml.transform.OutputKeys;
9 import javax.xml.transform.Result;
10 import javax.xml.transform.Source;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerConfigurationException;
13 import javax.xml.transform.TransformerException;
14 import javax.xml.transform.TransformerFactory;
15 import javax.xml.transform.dom.DOMSource;
16 import javax.xml.transform.stream.StreamResult;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import android.os.Bundle;
20 import android.os.Environment;
21 import android.app.Activity;
22 import android.view.Menu;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.widget.Button;
26 import android.widget.EditText;
27
28 public class MainActivity extends Activity {
29 private EditText name;
30 private EditText email;
31 private Button but = null;
32
33 @Override
34 protected void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36 setContentView(R.layout.activity_main);
37 this.name = (EditText) findViewById(R.id.name);
38 this.email = (EditText) findViewById(R.id.email);
39 this.but = (Button) findViewById(R.id.but);
40 this.but.setOnClickListener(new OnClickListenerImpl());
41 }
42
43 private class OnClickListenerImpl implements OnClickListener {
44
45 @Override
46 public void onClick(View v) {
47 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
48 return;
49 }
50 File file = new File(Environment.getExternalStorageDirectory() + File.separator + "neusoft" + File.separator + "member1.xml");
51 if (!file.getParentFile().exists()) {
52 file.getParentFile().mkdirs();
53 }
54 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
55 DocumentBuilder builder = null;
56 try {
57 builder = factory.newDocumentBuilder();
58 } catch (ParserConfigurationException e) {
59 e.printStackTrace();
60 }
61 Document doc = null;
62 doc = builder.newDocument();
63 Element addresslist = doc.createElement("addresslist");
64 Element linkman = doc.createElement("linkman");
65 Element name = doc.createElement("name");
66 Element email = doc.createElement("email");
67 name.appendChild(doc.createTextNode(MainActivity.this.name.getText().toString()));
68 email.appendChild(doc.createTextNode(MainActivity.this.email.getText().toString()));
69 linkman.appendChild(name);
70 linkman.appendChild(email);
71 addresslist.appendChild(linkman);
72 doc.appendChild(addresslist);
73 TransformerFactory tf = TransformerFactory.newInstance();
74 Transformer t = null;
75 try {
76 t = tf.newTransformer();
77 } catch (TransformerConfigurationException e) {
78 e.printStackTrace();
79 }
80 t.setOutputProperty(OutputKeys.ENCODING, "utf-8");
81 DOMSource source = new DOMSource(doc);
82 StreamResult result = new StreamResult(file);
83 try {
84 t.transform(source, result);
85 } catch (TransformerException e) {
86 e.printStackTrace();
87 }
88 }
89 }
90 }