我有一個簡單的應用程序,我希望能夠保存的EditText控制的內容,以便用戶可以添加自由格式的文本,並將其存儲在一個文本文件的EditText「文本」到SD卡
我在main.xml中
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="text in here"
android:id="@+id/editText" android:layout_gravity="center"/>
控制和菜單項
<item android:id="@+id/menu_add"
android:icon="@drawable/ic_menu_add"
android:title="@string/menu_add"
android:showAsAction="ifRoom|withText"
android:onClick="addItem"
/>
隨着的onClick打電話給我的addItem無效
public void addItem(MenuItem menuItem) {
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
try {
FileWriter filenew = new FileWriter(sdcard + "/test.txt");
BufferedWriter bw = new BufferedWriter(filenew);
bw.write(menuItem.toString());
bw.close();
} catch (IOException e) {
//You'll need to add proper error handling here
}
}
目前menuItem.toString()可以理解只是返回字符串「添加」
我怎麼能訪問包含在的EditText控件中的文本,所以我可以添加到test.txt文件?
不要使用文件存儲(如果不是必要的話),最好使用SharedPreferences來存儲簡單數據。 http://developer.android.com/guide/topics/data/data-storage.html#pref –
我希望用戶能夠添加自由格式文本並將其存儲在文本文件中 – spences10