-
1. #3F5D7D
-
2. #279B61
-
3. #008AB8
-
4. #993333
-
5. #A3E496
-
6. #95CAE4
-
7. #CC3333
-
8. #FFCC33
-
9. #FFFF7A
- 10. #CC6699
页面布局文件中两个按钮,一个用于读取数据,一个用于写入数据。
其对应的java文件:
SharedPreferencesTest.java:
package org.helloword;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SharedPreferencesTest extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sharedpreference);
preferences=getSharedPreferences("crazy", MODE_PRIVATE);
editor=preferences.edit();
Button read=(Button)findViewById(R.id.read);
Button write=(Button)findViewById(R.id.write);
read.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
String time=preferences.getString("time", null);
int randNum=preferences.getInt("randNum", 0);
String result=time==null?"没有写入数据":"写入时间为:"+time+"\n上次生成的随机数为:"+randNum;
Toast.makeText(SharedPreferencesTest.this, result, 5000).show();
}
});
write.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"HH:mm:ss");
editor.putString("time",sdf.format(new Date()) );
editor.putInt("randNum", (int) (Math.random()*100));
editor.commit();
}
});
}
}
要读写其他应用的SharedPreferences,可按如下步骤进行。
1。需要创建其他程序对应的Context,例如如下代码:
userCount=createPackageContext("com.example.wenandroid",Context.CONTEXT_IGNORE_SECURITY);
上面的程序中com.example.wenandroid就是其他应用程序的包名--实际上Android系统就是用应用程序的包名来作为该程序的标识的。
2。调用其他应用程序的Context的getSharedPreferences(String name,int mode)即可获取相应的SharedPreferences对象。
3。如果需要向其他应用程序的SharedPreferences数据写入数据,调用SharedPreferences的edit()方法获取相应的Editor即可。
例:
Context useCount=null;
useCount=createPackageContext("com.example.wenandroid",Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs=useCount.getSharedPreferences("count",Context.MODE_WORLD_READABLE);
int a=prefs.getInt("a",0);
在本文中主要介绍百度地图UI控制功能,即控制地图是否有缩放、平移、双击放大、旋转、俯视的功能以及控制是否显示内置缩放组件、指南针位置等。在文中采用标签监听使每个控制功能的方法见名知义,代码原型来源百度demo,代码如下:
Activity:
package com.home;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
/**
* 演示地图UI控制功能
*/
public class UISettingActivity extends Activity {
/**
* MapView 是地图主控件
*/
private MapView mMapView = null;
/**
* 用MapController完成地图控制
*/
private MapController mMapController = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 使用地图sdk前需先初始化BMapManager. BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,
* 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
*/
DemoApplication app = (DemoApplication) this.getApplication();
if (app.mBMapManager == null) {
app.mBMapManager = new BMapManager(this);
/**
* 如果BMapManager没有初始化则初始化BMapManager
*/
app.mBMapManager.init(DemoApplication.strKey,
new DemoApplication.MyGeneralListener());
}
/**
* 由于MapView在setContentView()中初始化,所以它需要在BMapManager初始化之后
*/
setContentView(R.layout.uisetting_main);
mMapView = (MapView) findViewById(R.id.bmapView);
/**
* 获取地图控制器
*/
mMapController = mMapView.getController();
/**
* 设置地图是否响应点击事件
*/
mMapController.enableClick(true);
/**
* 设置地图缩放级别
*/
mMapController.setZoom(12);
/**
* 设置地图俯角
*/
mMapController.setOverlooking(-30);
/**
* 将地图移动至天安门
* 使用百度经纬度坐标,可以通过http://api.map.baidu.com/lbsapi/getpoint/index
* .html查询地理坐标 如果需要在百度地图上显示使用其他坐标系统的位置,请发邮件至mapapi@baidu.com申请坐标转换接口
*/
double cLat = 39.945;
double cLon = 116.404;
GeoPoint p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
mMapController.setCenter(p);
}
/**
* 是否启用缩放手势
*
* @param v
*/
public void setZoomEnable(View v) {
mMapController.setZoomGesturesEnabled(((CheckBox) v).isChecked());
}
/**
* 是否启用平移手势
*
* @param v
*/
public void setScrollEnable(View v) {
mMapController.setScrollGesturesEnabled(((CheckBox) v).isChecked());
}
/**
* 是否启用双击放大
*
* @param v
*/
public void setDoubleClickEnable(View v) {
mMapView.setDoubleClickZooming(((CheckBox) v).isChecked());
}
/**
* 是否启用旋转手势
*
* @param v
*/
public void setRotateEnable(View v) {
mMapController.setRotationGesturesEnabled(((CheckBox) v).isChecked());
}
/**
* 是否启用俯视手势
*
* @param v
*/
public void setOverlookEnable(View v) {
mMapController
.setOverlookingGesturesEnabled(((CheckBox) v).isChecked());
}
/**
* 是否显示内置绽放控件
*
* @param v
*/
public void setBuiltInZoomControllEnable(View v) {
mMapView.setBuiltInZoomControls(((CheckBox) v).isChecked());
}
/**
* 设置指南针位置,指南针在3D模式下自动显现
*
* @param view
*/
public void setCompassLocation(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.lefttop:
if (checked)
// 设置指南针显示在左上角
mMapController.setCompassMargin(100, 100);
break;
case R.id.righttop:
if (checked)
mMapController.setCompassMargin(mMapView.getWidth() - 100, 100);
break;
}
}
@Override
protected void onPause() {
/**
* MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
*/
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
/**
* MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
*/
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
/**
* MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
*/
mMapView.destroy();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMapView.onRestoreInstanceState(savedInstanceState);
}
}
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setZoomEnable"
android:text="缩放" />
<CheckBox
android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setScrollEnable"
android:text="平移" />
<CheckBox
android:id="@+id/doubleClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setDoubleClickEnable"
android:text="双击放大" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setRotateEnable"
android:text="旋转" />
<CheckBox
android:id="@+id/overlook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="setOverlookEnable"
android:text="俯视" />
<CheckBox
android:id="@+id/zoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:onClick="setBuiltInZoomControllEnable"
android:text="缩放控件" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="10dip"
android:text="指南针位置" />
<RadioGroup
android:id="@+id/RadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:text="指南针位置" >
<RadioButton
android:id="@+id/lefttop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="setCompassLocation"
android:text="左上角" />
<RadioButton
android:id="@+id/righttop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setCompassLocation"
android:text="右上角" />
</RadioGroup>
</LinearLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
Application类和Manifest同前文。
附上图片效果: