暴走漫画系列之高仿淘宝收货地址(附demo)
引语: 我是个程序猿,一天我坐在路边一边喝水一边苦苦检查bug。 这时一个乞丐在我边上坐下了,开始要饭,我觉得可怜,就给了他1块钱。 然后接着调试程序。他可能生意不好,就无聊的看看我在干什么,然后过了一会,他缓缓地指着我的屏幕说,这里少了个分号…
谁说咱们程序猿不能写出既幽默又能懂的博客呢,本人想推出一系列博文,可以给刚接触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"); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent arg0) { // TODO Auto-generated method stub //具体的操作 } }
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> landDivide = landDivideDB.queryAddress("superior=?", new String[]{"1"}); Iterator<LandDivide> 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) { // TODO Auto-generated method stub 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> landDivide = landDivideDB.queryAddress("name=?", new String[]{name}); Iterator<LandDivide> iterator= landDivide.iterator(); while(iterator.hasNext()){ LandDivide l = iterator.next(); code = l.getCode(); break; } List<LandDivide> landDivide_2 = landDivideDB.queryAddress("superior=?", new String[]{code}); Iterator<LandDivide> 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) { // TODO Auto-generated method stub qu.clear(); String name = shi.get(position); String code = null; shiStr = name; shengTxt3.setText(shengStr); shiTxt3.setText(name); List<LandDivide> landDivide = landDivideDB.queryAddress("name=?", new String[]{name}); Iterator<LandDivide> iterator= landDivide.iterator(); while(iterator.hasNext()){ LandDivide l = iterator.next(); code = l.getCode(); break; } List<LandDivide> landDivide_2 = landDivideDB.queryAddress("superior=?", new String[]{code}); if(landDivide_2!=null){ Iterator<LandDivide> 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) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub 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<AddressInfo> list = addressDB.queryAddress(); if(list!=null){ Iterator<AddressInfo> 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; } } };
小弟不才,参加了CSDN的博客大赛,在这里想请各位帮个忙,投下您尊贵的一票,您说都看到这里了,是不是,底下就是投票的位置了,就顺便投一下嘛(如果底下没有投一票的按钮,请点击下面的链接,进入投票页面,拉到页面底下即可投票).
投票地址:http://vote.blog.csdn.net/Article/Details?articleid=32936187
源码地址: http://download.csdn.net/detail/chillax_li/7531409
尊重原创,转载请注明出处:http://blog.csdn.net/chillax_li/article/details/32936187