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

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

如果写插入location:

	final class LocationHelper {
		private LocationListener mLocationListener;
		private WriteBlog activity;
		private LocationManager mLocationManager;
		// 默认延迟。单位:秒
		private int timeout;
		private Handler uiHandler;
		private boolean isLocated;
		private LocationHolder holder;
		private boolean startFetching;
		private String defaultDraftPath;
		
		LocationHelper(WriteBlog activity) {
			this.activity = activity;
			mLocationManager = (LocationManager) activity
					.getSystemService(Context.LOCATION_SERVICE);
			uiHandler = new Handler();
			timeout = 60;
			defaultDraftPath = activity.getFilesDir().getAbsolutePath()
					+ "/draft/location.dat";
		}
		
		void dispose() {
			closeAll();
			isLocated = false;
			startFetching = false;
			holder = null;
		}
		
		
		void openAllProviders() {
			if (startFetching || isLocated) { return; }

			requestProviders();
		}
		
		
		boolean isFetching() {
			return startFetching;
		}
		
		boolean isLocatedSucess() {
			return (holder != null) && holder.isUseful();
		}
		
		void closeAll() {
			if (mLocationListener != null) {
				mLocationManager.removeUpdates(getLocationListener());
				mLocationListener = null;
			}
		}
		
		
		private LocationListener getLocationListener() {
			if (mLocationListener == null) {
				mLocationListener = new LocationListener() {

					public void onLocationChanged(Location location) {
						final LocationHolder holder = new LocationHolder();
						holder.lat = location.getLatitude();
						holder.lon = location.getLongitude();
						System.out.println("====holder.lat="+holder.lat+"lon="+holder.lon);
						if (holder.isUseful()) {
							isLocated = true;
							LocationHelper.this.holder = holder;
							activity.onLocatedSuccess();
							closeAll();
							startFetching = false;
						}
					}

					public void onProviderDisabled(String provider) {
					}

					public void onProviderEnabled(String provider) {
					}

					public void onStatusChanged(String provider, int status,
							Bundle extras) {
					}
				};
			}
			return mLocationListener;
		}
		
		
		private void requestProviders() {
			final List<String> providers = mLocationManager.getProviders(true);
			boolean requestSuccess = false;
			if (providers.contains(LocationManager.GPS_PROVIDER)) {
				mLocationManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER, 0, 0,
						getLocationListener());
				requestSuccess = true;
			}

			if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
				mLocationManager.requestLocationUpdates(
						LocationManager.NETWORK_PROVIDER, 0, 0,
						getLocationListener());
				requestSuccess = true;
			}

			if (requestSuccess) {
				activity.pgLoadingLocation.setVisibility(View.VISIBLE);
				startFetching = true;
				if (timeout > 0) {
					uiHandler.postDelayed(new Runnable() {

						public void run() {
							if (!isLocated && (activity != null)
									&& !activity.isFinishing()) {
								closeAll();
								activity.onLocatedError();
								startFetching = false;
							}
						}

					}, timeout * 1000);
				}
			}
		}
		
	}


	
	final class LocationHolder implements Serializable {
		private static final long serialVersionUID = -8205421689204807445L;
		double lat;
		double lon;

		public boolean equals(Object o) {
			if (o == null) { return false; }

			if (o == this) { return true; }

			if (o.getClass() == getClass()) {
				final LocationHolder holder = (LocationHolder) o;
				if ((Math.abs(holder.lat - lat) < .001)
						&& (Math.abs(holder.lon - lon) < .001)) { return true; }
			}
			return false;
		}

		public int hashCode() {
			final StringBuffer builder = new StringBuffer();
			builder.append(lat).append(lon);
			return builder.hashCode();
		}


		boolean isUseful() {
			return islegal(lat) && islegal(lon);
		}

		private boolean islegal(double pos) {
			if ((pos > 1.) || (pos < -1.)) { return true; }
			return false;
		}
	}


如何插入表情可以参见:http://blog.csdn.net/baidu_nod/article/details/38310729

然后这里来关注点击事件

final class FaceClickHealper {
		private boolean isFaceDiaplay = false;
		private WriteBlog activity;

		boolean isFaceDiaplay() {
			return isFaceDiaplay;
		}

		<strong>void onClick(View v) {
			activity.setEmotionViewVisibility(!isFaceDiaplay);
			if (isFaceDiaplay) {
				activity.displayFaceImageSrc();
			}
			else {
				activity.displayKeyboardImageSrc();
			}
			activity.setInputMethodVisibility(isFaceDiaplay);
			changeDiaplayFlag();
		}//这个是点击事件</strong>

		boolean onFinish() {
			if (isFaceDiaplay) {
				activity.displayFaceImageSrc();
				activity.setEmotionViewVisibility(false);
				changeDiaplayFlag();
				return true;
			}
			return false;
		}

		private void changeDiaplayFlag() {
			isFaceDiaplay = !isFaceDiaplay;
		}

		public FaceClickHealper(WriteBlog activity) {
			super();
			this.activity = activity;
		}
	}

	void displayFaceImageSrc() {
		if (ibFace != null) {
			ibFace.setImageResource(R.drawable.btn_insert_face);
		}
	}
	
	void displayKeyboardImageSrc() {
		if (ibFace != null) {
			ibFace.setImageResource(R.drawable.btn_insert_keyboard);
		}
	}
	
	boolean setInputMethodVisibility(boolean visibility) {
		if ((mInputMethodManager != null) && (etMblog != null)) {
			if (visibility) {
				mInputMethodManager.showSoftInput(etMblog, 0);
			}
			else {
				if (mInputMethodManager.isActive(etMblog)) {
					mInputMethodManager.hideSoftInputFromWindow(
							etMblog.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
					
				}
			}
		}
		return false;
	}
	<pre name="code" class="java"><span style="white-space:pre">	</span>void setEmotionViewVisibility(boolean visibility) {
		if (mEmotionView != null) {
			mEmotionView.setVisibility(visibility ? View.VISIBLE : View.GONE);
		}
	}