編輯:關於Android編程
首先增加權限
主要的activity
public class MainActivity extends Activity {
private EditText et1;
private EditText et2;
private Button bt1;
private Button bt2;
final String FILE_NAME = "crazyit.bin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
write(et1.getText().toString());
et1.setText("");
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
et2.setText(read());
}
});
}
private String read() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdcardDir = Environment.getExternalStorageDirectory();
FileInputStream fis = openFileInput(sdcardDir
.getCanonicalPath() + FILE_NAME);
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void write(String content) {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdcardDir = Environment.getExternalStorageDirectory();
File targetFile = new File(sdcardDir.getAbsoluteFile()
.getCanonicalPath() + FILE_NAME);
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
raf.seek(targetFile.length());
raf.write(content.getBytes());
raf.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
一、RatingBar簡單介紹RatingBar是基於SeekBar(拖動條)和ProgressBar(狀態條)的擴展,用星形來顯示等級評定,在使用默認RatingBar
自己剛學Android的時候在這上面花了不少時間,資料沒少找。學習別人的“關鍵代碼”,自己寫起來不是缺這就是缺那的。希望後來的同學在這上面少浪費些時間。 其實很簡單,就是
本篇總結了Android開發常見過程中的內存洩漏問題。集合類洩漏??集合類如果僅僅有添加元素的方法,而沒有相應的刪除機制,導致內存被占用。如果這個集合類是全局性的變量 (
在開發中發現一個問題:當一個我通過Intent開啟一個前面已經打開的activty的界面時,新打開的activity的狀態會丟失。當時,當我直接按home減將acitvi