編輯:關於Android編程
效果圖:

.java文件有MainActivity.java、FileService.java、FileServiceTest.java, .xml文件有activity_main.xml。
<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+sb60ztei1thBbmRyb2lkVGVzdENhc2XA4LXEyrnTw6Os1Nq/qrei1tC3x7OjyrXTw6Gj08PT2rLiytTEs9K7uabE3KGjPC9wPgo8cD7KudPDQW5kcm9pZFRlc3RDYXNlwOCjrNPQyOfPwrXE0qrH86O6PC9wPgo8cD4xLtTaQW5kcm9pZE1hbmlmZXN0LnhtbM7EvP7W0KOsPG1hbmlmZXN0PjwvbWFuaWZlc3Q+1tDM7bzTyOfPwqO6PC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">
2.在AndroidManifest.xml文件中, 中添加如下:
MainAcitity.java
package com.example.l3_files;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import java.io.IOException;
import com.example.l3_files.model.FileService;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private FileService fileService;
private Button saveButton;
NotificationManager notificationManager;
Notification notification;
PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileService = new FileService(this);
saveButton = (Button) this.findViewById(R.id.save);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
pendingIntent = PendingIntent
.getActivity(MainActivity.this, 0, null, 0);
notification = new Notification();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText fileNameText = (EditText) findViewById(R.id.filename);
EditText fileContentText = (EditText) findViewById(R.id.filecontent);
String fileName = fileNameText.getText().toString();
String fileContent = fileContentText.getText().toString();
try {
fileService.save(fileName, fileContent);
Toast.makeText(MainActivity.this, R.string.success,
Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.failure,
Toast.LENGTH_LONG).show();
}
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "文件保存成功";
notification.setLatestEventInfo(MainActivity.this, fileName,
fileContent, pendingIntent);
notificationManager.notify(0, notification);
}
});
}
}
package com.example.l3_files.model;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
/**
* 保存文件
* @param filename 文件名稱
* @param filecontent 文件內容
* @throws IOException
*/
public void save(String filename,String filecontent) throws IOException{
//第1個參數是文件的名稱,第2個參數是操作模式
FileOutputStream fos=context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
fos.write(filecontent.getBytes());
fos.close();
}
public String readFile(String filename) throws IOException{
FileInputStream fis=context.openFileInput(filename);
int len=0;
byte[] buffer=new byte[1024];
ByteArrayOutputStream baos=new ByteArrayOutputStream();//往內存中輸出數據的
while((len=fis.read(buffer))!=-1)//如果數據量很大,第2次讀取的數據有可能會把第1次讀取的數據給覆蓋掉
{
baos.write(buffer,0,len);
}
byte[] data=baos.toByteArray();//得到內存中的數據 以二進制存放的
baos.close();
fis.close();
return new String(data);//根據二進制數據轉換成所對應的字符串
}
}
package com.example.l3_files.model;
import java.io.IOException;
import android.test.AndroidTestCase;
public class FileServiceTest extends AndroidTestCase { //測試單元
public void testSave() throws IOException {
FileService fileService=new FileService(getContext());
fileService.save("file1.txt", "保存測試");
}
public void testReadFile() throws IOException {
FileService fileService=new FileService(getContext());
String content=fileService.readFile("file1.txt");
System.out.println(content);
}
}
activity_main.xml
測試類的使用:
右鍵已編寫的測試方法,Run as->Android JUnit Test.

如下測試成功,如果Errors不為0,則測試失敗。

Android UI之RealtiveLayout(相對布局)
Android UI之RealtiveLayout(相對布局)說明:RealtiveLayout是目前android開發中最最常用的布局管理器(比LinearLayout
Android的事件機制
一、理論概述最基本的操作類型:down 手指按下 move 手指在屏幕上移動 up 手指從屏幕上離開觸屏操作的順序:down->move->move->
Android游戲開發之黑白棋
黑白棋介紹黑白棋,又叫蘋果棋,最早流行於西方國家。游戲通過相互翻轉對方的棋子,最後以棋盤上誰的棋子多來判斷勝負。黑白棋非常易於上手,但精通則需要考慮許多因素,比如角邊這樣
TextView實戰
寫在前面的話對於TextView,我想大家都已經熟的不能再熟了。但是它的用法我們真的熟麼?為了避免總是一言不合就去翻官方文檔,在這裡我總結一下我也可能是你容易忽視的一些細