編輯:關於Android編程
打開main.xml修改內容如下:
File Settings 文件名稱 文件內容 保存 保存成功 保存失敗 讀取 讀取成功 讀取失敗
public class FileService { public Context context; public FileService(Context context){ this.context = context; } /** * 保存字符串到文件 * @param name 文件名 * @param content 文件內容 */ public void save(String name, String content) throws Exception{ // MODE_PRIVATE 創建出來的文件,僅能被本應用訪問,而且新寫入的內容會覆蓋原來的內容 FileOutputStream os = context.openFileOutput(name, Context.MODE_PRIVATE); // 默認保存在/data/data//files目錄 os.write(content.getBytes()); os.close(); } /*** * 讀取文件內容 * @param name 文件名 * @return * @throws Exception */ public String read(String name) throws Exception{ FileInputStream is = context.openFileInput(name); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while( (len = is.read(buf)) != -1){ os.write(buf,0, len); } byte[] data = os.toByteArray(); String content = new String(data); return content; } }
public EditText nameText; public EditText saveContentText; public EditText readContentText; public Button button_read; public String filename; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText = (EditText)this.findViewById(R.id.filename); saveContentText = (EditText)this.findViewById(R.id.save_file_content); readContentText = (EditText)this.findViewById(R.id.read_file_content); Button button_save = (Button) this.findViewById(R.id.button_save); button_save.setOnClickListener(new ButtonSaveClickListener()); button_read = (Button) this.findViewById(R.id.button_read); button_read.setClickable(false); button_read.setOnClickListener(new ButtonReadClickListener()); } private final class ButtonSaveClickListener implements View.OnClickListener{ public void onClick(View v){ String name = nameText.getText().toString(); String content = saveContentText.getText().toString(); FileService service = new FileService(getApplicationContext()); try { filename = name; service.save(name, content); filename = name; button_read.setClickable(true); Toast.makeText(getApplicationContext(),R.string.save_success, Toast.LENGTH_LONG).show(); }catch (Exception e){ Toast.makeText(getApplicationContext(),R.string.save_fail, Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } private final class ButtonReadClickListener implements View.OnClickListener{ public void onClick(View v){ FileService service = new FileService(getApplicationContext()); try { String content = service.read(filename); readContentText.setText(content); Toast.makeText(getApplicationContext(),R.string.read_success, Toast.LENGTH_LONG).show(); }catch (Exception e){ Toast.makeText(getApplicationContext(),R.string.read_fail, Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }
今天來講講自定義單個控件,就拿開關按鈕來講講,相信大家見了非常多這樣的了,先看看效果:我們可以看到一個很常見的開關按鈕,那就來分析分析。首先:這是由兩張圖片構成:①一張為
《Struck:Structured Output Tracking with Kernels》是Sam Hare, Amir Saffari, Philip H. S.
一加3怎麼刷入第三方recovery?一加3最近挺熱門的,很多人買了手機回來第一時間也是要刷各種第三方rom如CM,那麼一加3怎麼刷入第三方recovery
前言由於公司環境惡劣,小菜鳥我本來想畫UML圖來顯示類與類之間的關系,可惜這個念頭無法達成,也只好用Word文檔來完成。待菜鳥我辭職了,再自己畫上UML圖和Gif動態圖,