首页 > 代码库 > 如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location,代码全) (三)
如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入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的path public 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 block e.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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。