码迷,mamicode.com
首页 > 其他好文 > 详细

如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location,代码全) (三)

时间:2014-08-03 10:15:45      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:发微博

如果插入话题和插入@符号:

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下载

如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location,代码全) (三),布布扣,bubuko.com

如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location,代码全) (三)

标签:发微博

原文地址:http://blog.csdn.net/baidu_nod/article/details/38355867

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!