如何寫一個發微博的頁面(包括插入圖片,插入表情,插入話題,插入Location,代碼全) (三),

來源:互聯網
上載者:User

如何寫一個發微博的頁面(包括插入圖片,插入表情,插入話題,插入Location,代碼全) (三),

如果插入話題和插入@符號:

final class TopicHelper {private static final String TOPIC = "#請插入話題名稱#";private WriteBlog activity;public void insertText() {final EditText editText = activity.etMblog;final int selectionStart = editText.getSelectionStart();final int secletionEnd = editText.getSelectionEnd();final String text = editText.getText().toString();if (selectionStart == -1 || secletionEnd == -1|| selectionStart > secletionEnd) {activity.etMblog.append(TopicHelper.TOPIC);final int totalLength = activity.etMblog.getText().toString().length();activity.etMblog.setSelection(totalLength - TopicHelper.TOPIC.length() + 1,totalLength - 1);}else {final StringBuilder sb = new StringBuilder();sb.append(text.substring(0, selectionStart));sb.append(TOPIC);sb.append(text.substring(secletionEnd));editText.setText(sb.toString());Selection.setSelection(editText.getText(), selectionStart + 1,selectionStart + TOPIC.length() - 1);}}TopicHelper(WriteBlog activity) {super();this.activity = activity;}}

final class AtEditHelper {private static final String AT = "@請輸入使用者名稱 ";private WriteBlog activity;public void insertText() {final EditText editText = activity.etMblog;final int selectionStart = editText.getSelectionStart();//擷取游標所在位置final int secletionEnd = editText.getSelectionEnd();final String text = editText.getText().toString();if (selectionStart == -1 || secletionEnd == -1|| selectionStart > secletionEnd) {activity.etMblog.append(AtEditHelper.AT);final int totalLength = activity.etMblog.getText().toString().length();activity.etMblog.setSelection(totalLength - AtEditHelper.AT.length() + 1,totalLength - 1);}else {final StringBuilder sb = new StringBuilder();sb.append(text.substring(0, selectionStart));sb.append(AtEditHelper.AT);sb.append(text.substring(secletionEnd));editText.setText(sb.toString());Selection.setSelection(editText.getText(), selectionStart + 1,selectionStart + AtEditHelper.AT.length() - 1);}}AtEditHelper(WriteBlog activity) {super();this.activity = activity;}}

退出寫微博這頁的時候,我們要儲存圖片和內容:

//DEFAULT_DRAFT_TEXT_PATH = getFilesDir().getAbsolutePath()+ "/draft/text.dat";private boolean saveDraft(String text) {try {File f = new File(DEFAULT_DRAFT_TEXT_PATH);FileUtils.makesureFileExist(DEFAULT_DRAFT_TEXT_PATH);FileUtils.saveObject(text, new FileOutputStream(DEFAULT_DRAFT_TEXT_PATH));}catch (final Exception e) {}return false;}//這個方法是保證path存在public static void makesureFileExist(String path) {String separator = File.separator;int index = path.lastIndexOf(separator);path = path.substring(0,index); File f = new File(path);f.mkdirs();File f1 = new File(path);try {f1.createNewFile();} catch (IOException e) {e.printStackTrace();}}public static void saveObject(String text, FileOutputStream fos) {try {ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(text);oos.flush();oos.close();} catch (IOException ex) {ex.printStackTrace();}}

儲存圖片:

// path1和path2都應該是File的pathpublic static void copy(String inputFilePath, String path2) {String separator = File.separator;int index = path2.lastIndexOf(separator);String path = path2.substring(0, index);File temp = new File(path);if (!temp.exists()) {temp.mkdirs();}File tempFile = new File(path2);try {tempFile.createNewFile();} catch (IOException e1) {e1.printStackTrace();}FileInputStream inputStream = null;FileOutputStream outputStream = null;try {inputStream = new FileInputStream(inputFilePath);outputStream = new FileOutputStream(path2);byte buffer[] = new byte[4 * 1024];int len = -1;while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}outputStream.flush();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}

//defaultDraftPath = activity.getFilesDir().getAbsolutePath()+ "/draft/bitmap_temp.jpg";//  defaultTempPath = activity.getFilesDir().getAbsolutePath()+ "/pic/" + System.currentTimeMillis() + ".jpg";void saveDraft() {FileUtils.copy(defaultTempPath, defaultDraftPath);}

再次進入這個頁面的時候load頁面內容:

private void loadDraft() {String str = FileUtils.loadObject(DEFAULT_DRAFT_TEXT_PATH);etMblog.setText(str);etMblog.setSelection(str.length());if (mImageLoadingHelper.doesDraftExist()) {mImageLoadingHelper.loadDraft();}}public static String loadObject(String str) {    Object obj = null; File f = new File(str);try {FileInputStream fis = new FileInputStream(f);ObjectInputStream ois = new ObjectInputStream(fis);obj = ois.readObject();} catch (Exception ex) {ex.printStackTrace();}return (String)obj;}void loadDraft() {FileUtils.copy(defaultDraftPath, defaultTempPath);activity.displayInsertBitmap();}

代碼可以在http://download.csdn.net/detail/baidu_nod/7708705下載




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.