谁说咱们攻城狮不能写出既幽默又能懂的博客呢,本人想推出一系列博文,可以给刚接触Android开发的做一个参考,也可以与接触Android已久的各路大神比较一下,本人喜欢交流,如果有写得不好的地方,欢迎大家指出.如果有更好的效果或者功能,希望大家多多指点,互相学习可以共同进步.
说了这么多,现在进入今天的猪蹄,推出<<暴走漫画系列--高仿淘宝收货地址>>,有网购经验的同学应该对这个很熟悉,在淘宝的Android客户端里面,有一个功能是设置收货地址,不清楚的同学可以自己下载个淘宝客户端,自己添加一下看看.
本人一向喜欢贴图,这样有图有真相,才能说服每个人,废话不多说先上自己程序启动画面,然后运行界面,最后一张是淘宝客户端对照图.
图片震楼 启动界面 运行界面 淘宝界面
淘宝客户端无法录屏所以这里只能上静态的,大家可以自己打开淘宝客户端看看.
下面进行详细的代码讲解:
(一)本人在程序启动的时候开了一条后台服务landDivideServeice,目的是为了将全国的省市县总共4,189条数据插入到手机数据库里面去,原本的数据是保存在7个txt里面的,由于放在txt里面,读取操作很不方便,所以我把它放到数据库,在后面的地区筛选很快就出来,这个比淘宝还快哦,不信你们可以试一下.
这里要注意的一点是landDivideServeice是继承IntentService的,不是直接继承Service的.
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。
简单的说IntentService类似于一条后台线程,但操作完成时,我们不用手动去关闭Serveice,他会自动关闭.MyIntentService继承IntentService,之后必须实现一个无参数的构造方法MyIntentService(),然后还要重写onHandleIntent(Intent intent);此后所有的后台操作即可在onHandleIntent(Intent intent)执行;
- public class MyIntentService extends IntentService {
-
-
- public MyIntentService() {
- super("MyIntentService");
-
- }
-
- @Override
- protected void onHandleIntent(Intent arg0) {
-
-
- }
-
- }
so ga...
IntentService详细解析:
http://blog.csdn.net/ryantang03/article/details/8146154
http://blog.csdn.net/flowingflying/article/details/7616333
(二)在程序启动的第一个activity里,我弄了一条倒计时线程,时间总共是20秒,目的是想为数据写入数据库争取多一些时间,如果你进入地址筛选界面,看到数据不全或者报错,是因为数据还没有写完.
实现倒计时功能主要是继承CountDownTimer类,重写onTick和onFinsh这两个方法,onFinish()中的代码是计时器结束的时候要做的事情;onTick(Long m)中的代码是你倒计时开始时要做的事情,构造方法TimeCount()中的两个参数中,前者是倒计的时间数,后者是倒计每秒中间 的间隔时间,都是以毫秒为单位.例如要倒计时30秒,每秒中间间隔时间是1秒,两个参数可以这样写TimeCount(20000,1000).
- TimeCount time = new TimeCount(20000, 1000);
- time.start();
-
- class TimeCount extends CountDownTimer {
- public TimeCount(long millisInFuture, long countDownInterval) {
- super(millisInFuture, countDownInterval);
- }
-
- @Override
- public void onFinish() {
- next = 0;
- }
-
- @Override
- public void onTick(long millisUntilFinished) {
- }
- }
(三)剩下的主要是一些界面的切换跟数据的读取,只要思路正确是不会被搞混的.
- landDivideDB = LandDivideDB.getInstance(getBaseContext());
- List landDivide = landDivideDB.queryAddress("superior=?", new String[]{"1"});
- Iterator iterator = null;
- if(landDivide!=null){
- iterator = landDivide.iterator();
-
- while(iterator.hasNext()){
- LandDivide l = iterator.next();
- sheng.add(l.getName());
- }
- }else{
- return;
- }
- mListView1.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView> arg0, View arg1, int position,
- long arg3) {
-
- mLinearLayout1.setVisibility(View.GONE);
- mLinearLayout2.setVisibility(View.VISIBLE);
- mLinearLayout3.setVisibility(View.GONE);
-
- shi.clear();
- String name = sheng.get(position);
- String code = null;
- shengStr = name;
- shengTxt2.setText(name);
-
- List landDivide = landDivideDB.queryAddress("name=?", new String[]{name});
- Iterator iterator= landDivide.iterator();
- while(iterator.hasNext()){
- LandDivide l = iterator.next();
- code = l.getCode();
- break;
- }
-
- List landDivide_2 = landDivideDB.queryAddress("superior=?", new String[]{code});
- Iterator iterator_2= landDivide_2.iterator();
- while(iterator_2.hasNext()){
- LandDivide l = iterator_2.next();
- shi.add(l.getName());
- }
-
- shiAdapter.notifyDataSetChanged();
- quAdapter.clear();
- quAdapter.notifyDataSetChanged();
- }
- });
-
- mListView2.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView> arg0, View arg1, int position,
- long arg3) {
-
-
- qu.clear();
-
- String name = shi.get(position);
- String code = null;
-
- shiStr = name;
- shengTxt3.setText(shengStr);
- shiTxt3.setText(name);
-
- List landDivide = landDivideDB.queryAddress("name=?", new String[]{name});
-
- Iterator iterator= landDivide.iterator();
- while(iterator.hasNext()){
- LandDivide l = iterator.next();
- code = l.getCode();
- break;
- }
-
-
- List landDivide_2 = landDivideDB.queryAddress("superior=?", new String[]{code});
- if(landDivide_2!=null){
- Iterator iterator_2= landDivide_2.iterator();
- while(iterator_2.hasNext()){
- LandDivide l = iterator_2.next();
- qu.add(l.getName());
- }
- }
-
-
-
- quAdapter.notifyDataSetChanged();
-
- if(qu.size()<1){
- mLinearLayout1.setVisibility(View.GONE);
- mLinearLayout2.setVisibility(View.VISIBLE);
- mLinearLayout3.setVisibility(View.GONE);
-
- Intent i = new Intent(AddressChoose.this,BuyAddress.class);
- i.putExtra("address", shengStr+","+shiStr);
- startActivity(i);
- finish();
-
- }else{
-
- mLinearLayout1.setVisibility(View.GONE);
- mLinearLayout2.setVisibility(View.GONE);
- mLinearLayout3.setVisibility(View.VISIBLE);
-
- mListView3.setVisibility(View.VISIBLE);
- topView3.setText("请选择 县区/其他...");
- }
- }
- });
-
- mListView3.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView> arg0, View arg1, int position,
- long arg3) {
-
-
- String name = qu.get(position);
-
- quStr = name;
-
- Intent i2 = new Intent(AddressChoose.this,BuyAddress.class);
- i2.putExtra("address", shengStr+" "+shiStr+" "+quStr);
- startActivity(i2);
- finish();
- }
- });
(四)这个是选择好地址之后的界面,主要还是界面的转换跟焦点的获取
有时候setFocusable(true)跟setFocusableInTouchMode(true)并不能使控件获得焦点,要再之前加上一句requestFocus(),这样控件就可以获得焦点.
- OnFocusChangeListener focusChanger = new OnFocusChangeListener() {
-
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
-
- myAddress.setStreet(jiequText.getText().toString());
- myAddress.setName(nameText.getText().toString());
- myAddress.setPhone(phoneText.getText().toString());
-
- switch (v.getId()) {
- case R.id.my_set_buyaddress_jiequ:
- if (!hasFocus && myAddress.getStreet().length() > 0) {
- jiequText.setVisibility(View.GONE);
- jiequLinear.setVisibility(View.VISIBLE);
-
- jiequTextView.setText(myAddress.getStreet());
- }
-
- if (hasFocus) {
- jiequText.setSelectAllOnFocus(true);
- }
- break;
- case R.id.my_set_buyaddress_name:
- if (!hasFocus && myAddress.getName().length() > 0) {
- nameText.setVisibility(View.GONE);
- nameLinear.setVisibility(View.VISIBLE);
-
- nameTextView.setText(myAddress.getName());
- }
-
- if (hasFocus) {
- nameText.setSelectAllOnFocus(true);
- }
- break;
- case R.id.my_set_buyaddress_phone:
- if (!hasFocus && myAddress.getPhone().length() > 0) {
- phoneText.setVisibility(View.GONE);
- phoneLinear.setVisibility(View.VISIBLE);
-
- phoneTextView.setText(myAddress.getPhone());
- }
- if (hasFocus) {
- phoneText.setSelectAllOnFocus(true);
- }
- break;
-
- default:
- break;
- }
- }
- };
-
- OnClickListener click = new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- switch (v.getId()) {
- case R.id.my_set_buyaddress_sheng_linear:
- Intent i = new Intent(BuyAddress.this, AddressChoose.class);
- i.putExtra("Boolean", "aaa");
- startActivity(i);
- break;
- case R.id.my_set_buyaddress_jiequ_linear:
- jiequText.setVisibility(View.VISIBLE);
- jiequLinear.setVisibility(View.GONE);
-
- jiequText.setFocusable(true);
- jiequText.setFocusableInTouchMode(true);
-
- jiequText.requestFocus();
- break;
- case R.id.my_set_buyaddress_name_linear:
- nameText.setVisibility(View.VISIBLE);
- nameLinear.setVisibility(View.GONE);
-
- nameText.setFocusable(true);
- nameText.setFocusableInTouchMode(true);
-
- nameText.requestFocus();
- break;
- case R.id.my_set_buyaddress_phone_linear:
- phoneText.setVisibility(View.VISIBLE);
- phoneLinear.setVisibility(View.GONE);
-
- phoneText.setFocusable(true);
- phoneText.setFocusableInTouchMode(true);
-
- phoneText.requestFocus();
- break;
- case R.id.my_set_buyaddress_address_btn:
- myAddress.setStreet(jiequText.getText().toString());
- myAddress.setName(nameText.getText().toString());
- myAddress.setPhone(phoneText.getText().toString());
-
- if (myAddress.getPhone().length() > 0) {
- phoneText.setVisibility(View.GONE);
- phoneLinear.setVisibility(View.VISIBLE);
- phoneTextView.setText(myAddress.getPhone());
- }
- postBtn.requestFocus();
-
- postBtn.setFocusable(true);
- postBtn.setFocusableInTouchMode(true);
-
- if (myAddress.getProvinces().length() < 1 || myAddress.getStreet().length() < 1
- || myAddress.getName().length() < 1 || myAddress.getPhone().length() < 1) {
- Toast.makeText(getBaseContext(), "请完整填写收货人资料", 0).show();
- return;
- }
-
- myAddress.setStatus(checkBox.isChecked());
- AddressDB addressDB = AddressDB.getInstance(getBaseContext());
-
- if(checkBox.isChecked()){
- List list = addressDB.queryAddress();
- if(list!=null){
- Iterator iterator = list.iterator();
- while(iterator.hasNext()){
- AddressInfo a = iterator.next();
- a.setStatus(false);
- addressDB.updeteAddress(a);
- }
- }
-
- }
-
-
- if (addressinfo != null) {
- if(addressDB.updeteAddress(myAddress)){
- Toast.makeText(getBaseContext(), "修改收货地址成功", Toast.LENGTH_LONG).show();
- }else{
- Toast.makeText(getBaseContext(), "修改收货地址失败", Toast.LENGTH_LONG).show();
- }
- } else {
-
- SimpleDateFormat format = new SimpleDateFormat(
- "yyyyMMddHHmmss");
- Date date = new Date();
- String id = format.format(date);
- myAddress.setId(id);
-
- if(addressDB.insertAddress(myAddress)){
- Toast.makeText(getBaseContext(), "添加收货地址成功", Toast.LENGTH_LONG).show();
- }else{
- Toast.makeText(getBaseContext(), "添加收货地址失败", Toast.LENGTH_LONG).show();
- }
- }
-
- Intent intent = new Intent(BuyAddress.this, PersonAddress.class);
- startActivity(intent);
- finish();
-
-
- break;
-
- case R.id.download_layout1:
- finish();
- break;
- default:
- break;
- }
- }
- };
更多的代码解析,大家可以下载我的demo,看一下哦
最后就这样了哈,有什么不懂的或者更好的建议的,可以提出来哈,欢迎大家指教
小弟不才,参加了CSDN的博客大赛,在这里想请各位帮个忙,投下您尊贵的一票,您说都看到这里了,是不是,底下就是投票的位置了,就顺便投一下嘛(如果底下没有投一票的按钮,请点击下面的链接,进入投票页面,拉到页面底下即可投票).
投票地址:http://vote.blog.csdn.net/Article/Details?articleid=32936187
源码地址: http://download.csdn.net/detail/chillax_li/7531409