当前位置: 编程技术>移动开发
本页文章导读:
▪百度map之Poi搜索功能 百度地图之Poi搜索功能该示例主要介绍关键词查询、suggestion查询和查看餐饮类Place详情页功能,尤其搜索某个地方的餐厅、理发店等等比较有实际意义,百度Demo代码如下:
Activity:
package com..........
▪ 中文字符ASCII码跟NSString相互转换 中文字符ASCII码和NSString相互转换在xcode中,文件以utf8格式保存。因此,其中变量对象也是以utf8格式保存。不同语言的utf8编码不一样,英文的utf8编码和ascii码一样。
不同语言的每个字符的utf8.........
▪ 怎么在自己的Activity中去控制EditText的焦点 如何在自己的Activity中去控制EditText的焦点在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好。所以一般会通过.........
[1]百度map之Poi搜索功能
来源: 互联网 发布时间: 2014-02-18
百度地图之Poi搜索功能
该示例主要介绍关键词查询、suggestion查询和查看餐饮类Place详情页功能,尤其搜索某个地方的餐厅、理发店等等比较有实际意义,百度Demo代码如下:
Activity:
package com.home;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionInfo;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
/**
* 演示poi搜索功能
*/
public class PoiSearchActivity extends Activity {
private MapView mMapView = null;
private MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
/**
* 搜索关键字输入窗口
*/
private AutoCompleteTextView keyWorldsView = null;
private ArrayAdapter<String> sugAdapter = null;
private int load_Index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoApplication app = (DemoApplication) this.getApplication();
if (app.mBMapManager == null) {
app.mBMapManager = new BMapManager(this);
app.mBMapManager.init(DemoApplication.strKey,
new DemoApplication.MyGeneralListener());
}
setContentView(R.layout.activity_poisearch);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.getController().enableClick(true);
mMapView.getController().setZoom(12);
// 初始化搜索模块,注册搜索事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapManager, new MKSearchListener() {
// 在此处理详情页结果
@Override
public void onGetPoiDetailSearchResult(int type, int error) {
if (error != 0) {
Toast.makeText(PoiSearchActivity.this, "抱歉,未找到结果",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PoiSearchActivity.this, "成功,查看详情页面",
Toast.LENGTH_SHORT).show();
}
}
/**
* 在此处理poi搜索结果
*/
public void onGetPoiResult(MKPoiResult res, int type, int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(PoiSearchActivity.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
// 将地图移动到第一个POI中心点
if (res.getCurrentNumPois() > 0) {
// 将poi结果显示到地图上
MyPoiOverlay poiOverlay = new MyPoiOverlay(
PoiSearchActivity.this, mMapView, mSearch);
poiOverlay.setData(res.getAllPoi());
mMapView.getOverlays().clear();
mMapView.getOverlays().add(poiOverlay);
mMapView.refresh();
// 当ePoiType为2(公交线路)或4(地铁线路)时, poi坐标为空
for (MKPoiInfo info : res.getAllPoi()) {
if (info.pt != null) {
mMapView.getController().animateTo(info.pt);
break;
}
}
} else if (res.getCityListNum() > 0) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = "在";
for (int i = 0; i < res.getCityListNum(); i++) {
strInfo += res.getCityListInfo(i).city;
strInfo += ",";
}
strInfo += "找到结果";
Toast.makeText(PoiSearchActivity.this, strInfo,
Toast.LENGTH_LONG).show();
}
}
public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int error) {
}
public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
}
public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
}
public void onGetAddrResult(MKAddrInfo res, int error) {
}
public void onGetBusDetailResult(MKBusLineResult result, int iError) {
}
/**
* 更新建议列表
*/
@Override
public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
if (res == null || res.getAllSuggestions() == null) {
return;
}
sugAdapter.clear();
for (MKSuggestionInfo info : res.getAllSuggestions()) {
if (info.key != null)
sugAdapter.add(info.key);
}
sugAdapter.notifyDataSetChanged();
}
@Override
public void onGetShareUrlResult(MKShareUrlResult result, int type,
int error) {
}
});
keyWorldsView = (AutoCompleteTextView) findViewById(R.id.searchkey);
sugAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line);
keyWorldsView.setAdapter(sugAdapter);
/**
* 当输入关键字变化时,动态更新建议列表
*/
keyWorldsView.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
if (cs.length() <= 0) {
return;
}
String city = ((EditText) findViewById(R.id.city)).getText()
.toString();
/**
* 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
*/
mSearch.suggestionSearch(cs.toString(), city);
}
});
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
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);
}
/**
* 影响搜索按钮点击事件
*
* @param v
*/
public void searchButtonProcess(View v) {
EditText editCity = (EditText) findViewById(R.id.city);
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
mSearch.poiSearchInCity(editCity.getText().toString(), editSearchKey
.getText().toString());
}
public void goToNextPage(View v) {
// 搜索下一组poi
int flag = mSearch.goToPoiPage(++load_Index);
if (flag != 0) {
Toast.makeText(PoiSearchActivity.this, "先搜索开始,然后再搜索下一组数据",
Toast.LENGTH_SHORT).show();
}
}
}
布局XML(activity_poisearch):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在" >
</TextView>
<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="北京" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="市内找" >
</TextView>
<AutoCompleteTextView
android:id="@+id/searchkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.88"
android:text="餐厅" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="searchButtonProcess"
android:padding="10dip"
android:text="开始" />
<Button
android:id="@+id/map_next_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="goToNextPage"
android:padding="10dip"
android:text="下一组数据" />
</LinearLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
MyPoiOverlay类:
package com.home;
import android.app.Activity;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKSearch;
public class MyPoiOverlay extends PoiOverlay {
MKSearch mSearch;
public MyPoiOverlay(Activity activity, MapView mapView, MKSearch search) {
super(activity, mapView);
mSearch = search;
}
@Override
protected boolean onTap(int i) {
super.onTap(i);
MKPoiInfo info = getPoi(i);
if (info.hasCaterDetails) {
mSearch.poiDetailSearch(info.uid);
}
return true;
}
}
在配置文件中要比之前多配置一个activity,不然没法查看详细界面,这是百度SDK jar中提供的类:
<activity
android:name="com.baidu.mapapi.search.PlaceCaterActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
Application类同之前。
附上图片效果:
详细界面:
[2] 中文字符ASCII码跟NSString相互转换
来源: 互联网 发布时间: 2014-02-18
中文字符ASCII码和NSString相互转换
在xcode中,文件以utf8格式保存。因此,其中变量对象也是以utf8格式保存。不同语言的utf8编码不一样,英文的utf8编码和ascii码一样。
不同语言的每个字符的utf8编码的字节数不一样,字节码也不一样。对于英文字符,查看它的ascii码,很方便,将字符取出来,就是它的ascii码。其实,对于非英文字符,取字符集编码的方式也是这样。这样统称为取ASCII码,在很多文档中也是这样描述的。
网上很多这样例子,介绍如何将字符和ASCII码相互转化。但是它们都没有提及如何转换中文等其他非英文的字符,使用这个方法都会转成乱码。
使用英文转换测试,如下所示:
// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65
// ASCII to NSString
int asciiCode = 65;
NSString *string = [NSString stringWithFormat:@"%c", asciiCode]; // A
再使用中文测试一下,使用[NSString stringWithFormat:@"%c", asciiCode]得到的是乱码字符,就是说根本没识别正确。
再说解决方法之前,先了解一下stringWithFormat方法中各种format。其中将ascii码转成字符有两种format,分别为%c和%C。
/*
%c
8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
%C
16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
*/
使用[NSString stringWithFormat:@"%C", asciiCode]就可以正常得到所要的字符。
分别以英文,中文和日文举例。
NSString *theString = @"g";
unichar theChar = [theString characterAtIndex:0];
NSString *theString1 = [NSString stringWithFormat:@"%c", theChar];
NSString *theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
theString = @"家";
theChar = [theString characterAtIndex:0];
theString1 = [NSString stringWithFormat:@"%c", theChar];
theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
theString = @"カントリー";
theChar = [theString characterAtIndex:2];
theString1 = [NSString stringWithFormat:@"%c", theChar];
theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=g,103,g,g
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=家,23478,?,家
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=カントリー,12488,?,ト
显示结果表明,这个方法是正确的。对于两个字节组成的字符,是能显示出的。不知道其他语言会怎么样,没有条件去测试。
在xcode中,文件以utf8格式保存。因此,其中变量对象也是以utf8格式保存。不同语言的utf8编码不一样,英文的utf8编码和ascii码一样。
不同语言的每个字符的utf8编码的字节数不一样,字节码也不一样。对于英文字符,查看它的ascii码,很方便,将字符取出来,就是它的ascii码。其实,对于非英文字符,取字符集编码的方式也是这样。这样统称为取ASCII码,在很多文档中也是这样描述的。
网上很多这样例子,介绍如何将字符和ASCII码相互转化。但是它们都没有提及如何转换中文等其他非英文的字符,使用这个方法都会转成乱码。
使用英文转换测试,如下所示:
// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65
// ASCII to NSString
int asciiCode = 65;
NSString *string = [NSString stringWithFormat:@"%c", asciiCode]; // A
再使用中文测试一下,使用[NSString stringWithFormat:@"%c", asciiCode]得到的是乱码字符,就是说根本没识别正确。
再说解决方法之前,先了解一下stringWithFormat方法中各种format。其中将ascii码转成字符有两种format,分别为%c和%C。
/*
%c
8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
%C
16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit.
*/
使用[NSString stringWithFormat:@"%C", asciiCode]就可以正常得到所要的字符。
分别以英文,中文和日文举例。
NSString *theString = @"g";
unichar theChar = [theString characterAtIndex:0];
NSString *theString1 = [NSString stringWithFormat:@"%c", theChar];
NSString *theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
theString = @"家";
theChar = [theString characterAtIndex:0];
theString1 = [NSString stringWithFormat:@"%c", theChar];
theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
theString = @"カントリー";
theChar = [theString characterAtIndex:2];
theString1 = [NSString stringWithFormat:@"%c", theChar];
theString2 = [NSString stringWithFormat:@"%C", theChar];
NSLog(@"theString=%@,%d,%@,%@",theString,theChar,theString1,theString2);
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=g,103,g,g
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=家,23478,?,家
2013-09-12 15:36:27.849 XYShopping[1892:18e03] theString=カントリー,12488,?,ト
显示结果表明,这个方法是正确的。对于两个字节组成的字符,是能显示出的。不知道其他语言会怎么样,没有条件去测试。
[3] 怎么在自己的Activity中去控制EditText的焦点
来源: 互联网 发布时间: 2014-02-18
如何在自己的Activity中去控制EditText的焦点
在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好。所以一般会通过代码控制让EditText不获取焦点。常用的方式如下,在Activity的布局文件中加上如下代码:
<!-- 输入焦点控制 -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
当点击Spinner时让EditText失去焦点即可:(见标红代码)
// 初始化两个LayoutInflater对象
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
et_name = (EditText) findViewById(R.id.et_name);
ib_add = (ImageButton) findViewById(R.id.ib_add);
ib_clear = (Button) findViewById(R.id.ib_clear);
ib_query = (Button) findViewById(R.id.ib_query);
ib_down = (Button) findViewById(R.id.ib_down);
ib_save = (Button) findViewById(R.id.ib_save);
ib_back = (ImageButton) findViewById(R.id.ib_back);
// ib_sex = (ImageButton) findViewById(R.id.ib_sex) ;
ib_add.setOnClickListener(this);
ib_clear.setOnClickListener(this);
ib_query.setOnClickListener(this);
ib_down.setOnClickListener(this);
ib_save.setOnClickListener(this);
ib_back.setOnClickListener(this);
// ib_sex.setOnClickListener(this) ;
// 性别
ArrayAdapter<String> sexAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
sexAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String sexString;
sexAdapter.clear();
int position = 0;
for (int i = 0; i < sexList.length; i++) {
sexString = sexList[i];
sexAdapter.add(sexString);
}
sp_sex = (Spinner) findViewById(R.id.sp_sex);
sp_sex.setAdapter(sexAdapter);
sp_sex.setFocusable(true);
sp_sex.setSelection(position);
sp_sex.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_sex.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_month.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_customerlevel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 出生月份
ArrayAdapter<String> monthAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String monthString;
monthAdapter.clear();
position = 0;
for (int i = 0; i < monthList.length; i++) {
monthString = monthList[i];
monthAdapter.add(monthString);
}
sp_month = (Spinner) findViewById(R.id.sp_month);
sp_month.setAdapter(monthAdapter);
sp_month.setFocusable(true);
sp_month.setSelection(position);
sp_month.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 客户等级
ArrayAdapter<String> customerlevelAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
customerlevelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String customerlevelString;
customerlevelAdapter.clear();
position = 0;
for (int i = 0; i < customerlevelList.length; i++) {
customerlevelString = customerlevelList[i];
customerlevelAdapter.add(customerlevelString);
}
sp_customerlevel = (Spinner) findViewById(R.id.sp_customerlevel);
sp_customerlevel.setAdapter(customerlevelAdapter);
sp_customerlevel.setFocusable(true);
sp_customerlevel.setSelection(position);
sp_customerlevel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 查询所有的客户资料
mCurrentData.clear();
mCurrentData = CustomerDBManager.getInstance().queryCustomerAll();
mAdapter = new SessionLocalAdapter(mCurrentData, CustomerMainActivity.this);
mListView = getListView();
mListView.setAdapter(mAdapter);
mListView.setFooterDividersEnabled(false); // 设成flase时,此ListView将不会在页脚视图前画分隔符。此属性缺省值为true。如果listview中没有添加页脚这个属性就不起作用。
mListView.setHeaderDividersEnabled(false); // 设成flase时,此ListView将不会在页眉视图前画分隔符。此属性缺省值为true。如果listview中没有添加页眉这个属性就不起作用。
mListView.setOnItemClickListener(this);
mListView.setOnItemLongClickListener(this);
最新技术文章: