首页 > 代码库 > 暴走系列--高仿淘宝收货地址
暴走系列--高仿淘宝收货地址
谁说咱们攻城狮不能写出既幽默又能懂的博客呢,本人想推出一系列博文,可以给刚接触Android开发的做一个参考,也可以与接触Android已久的各路大神比较一下,本人喜欢交流,如果有写得不好的地方,欢迎大家指出.如果有更好的效果或者功能,希望大家多多指点,互相学习可以共同进步.
说了这么多,现在进入今天的猪蹄,<<暴走系列--高仿淘宝收货地址>>,有网购经验的同学应该对这个很熟悉,在淘宝的Android客户端里面,有一个功能是设置收货地址,不清楚的同学可以自己下载个淘宝客户端,自己添加一下看看.
本人一向喜欢贴图,这样有图有真相,才能说服每个人,废话不多说先上自己程序启动画面,然后运行界面,最后一张是淘宝客户端对照图.
图片震楼 启动界面 运行界面 淘宝界面
淘宝客户端无法录屏所以这里只能上静态的,大家可以自己打开淘宝客户端看看.
下面进行详细的代码讲解:
(一)本人在程序启动的时候开了一条后台服务landDivideServeice,目的是为了将全国的省市县总共4,189条数据插入到手机数据库里面去,原本的数据是保存在7个txt里面的,由于放在txt里面,读取操作很不方便,所以我把它放到数据库,在后面的所属地区筛选很快就出来,这个比淘宝还快哦,不信你们可以试一下.
有个新的知识点就是landDivideServeice是继承IntentService的,不是直接继承Service的,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。
so ga...
IntentService详细解析:http://blog.csdn.net/ryantang03/article/details/8146154
/** * 主要的目的就是插入地址数据到数据库 * @author kun * */ public class landDivideServeice extends IntentService { public landDivideServeice() { super("landDivideServeice"); // TODO Auto-generated constructor stub } private String[] txt = new String[] { "city1.txt", "city2.txt", "city3.txt", "city4.txt", "city5.txt", "city6.txt", "city7.txt" }; @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub LandDivideDB landDivideDB = LandDivideDB .getInstance(getBaseContext()); List<LandDivide> list = landDivideDB.queryAddress(null, null); if (list == null) { for (int i = 0; i < txt.length; i++) { String str = getAssetString(txt[i], getBaseContext()); try { str = str.substring(1); JSONArray jsonArray = new JSONArray(str); for (int j = 0; j < jsonArray.length(); j++) { JSONObject temp = jsonArray.getJSONObject(j); LandDivide landDivide = new LandDivide(); landDivide.setCode(temp.getString("code")); landDivide.setName(temp.getString("name")); landDivide.setSuperior(temp .getString("superior")); landDivideDB.insertAddress(landDivide); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { for (int i = 0; i < txt.length; i++) { String str = getAssetString(txt[i], getBaseContext()); try { str = str.substring(1); JSONArray jsonArray = new JSONArray(str); for (int j = 0; j < jsonArray.length(); j++) { JSONObject temp = jsonArray.getJSONObject(j); LandDivide landDivide = new LandDivide(); landDivide.setCode(temp.getString("code")); landDivide.setName(temp.getString("name")); landDivide.setSuperior(temp .getString("superior")); List<LandDivide> mList = landDivideDB.queryAddress("code=?", new String[]{temp.getString("code")}); if(mList==null){ landDivideDB.insertAddress(landDivide); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static String getAssetString(String asset, Context context) { BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new InputStreamReader(context .getAssets().open(asset), "utf-8")); String line = null; StringBuilder builder = new StringBuilder(); while (null != (line = bufferedReader.readLine())) { builder.append(line).append("\n"); } return builder.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } bufferedReader = null; } return ""; } }
(二)剩下的主要是一些界面的切换跟数据的读取,只要思路正确是不会被搞混的.
public class AddressChoose extends Activity { private ListView mListView1; private ListView mListView2; private ListView mListView3; private LinearLayout mLinearLayout1; private LinearLayout mLinearLayout2; private LinearLayout mLinearLayout3; private LinearLayout onBackLinear; private TextView topTxt; private List<String> sheng = new ArrayList<String>(); private List<String> shi = new ArrayList<String>(); private List<String> qu = new ArrayList<String>(); private ArrayAdapter<String> shengAdapter; private ArrayAdapter<String> shiAdapter; private ArrayAdapter<String> quAdapter; private TextView shengTxt2; private TextView shengTxt3,shiTxt3,topView3; private static String shengStr,shiStr,quStr; private LandDivideDB landDivideDB; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.my_set_addresschoose); mLinearLayout1 = (LinearLayout)this.findViewById(R.id.my_set_adresschoose_1); mLinearLayout2 = (LinearLayout)this.findViewById(R.id.my_set_adresschoose_2); mLinearLayout3 = (LinearLayout)this.findViewById(R.id.my_set_adresschoose_3); mLinearLayout1.setVisibility(View.VISIBLE); mLinearLayout2.setVisibility(View.GONE); mLinearLayout3.setVisibility(View.GONE); topTxt = (TextView) this.findViewById(R.id.txt_linear); onBackLinear = (LinearLayout) this.findViewById(R.id.download_layout1); topTxt.setText("添加收货地址"); shengTxt2 = (TextView)this.findViewById(R.id.my_set_adresschoose_sheng_2); shengTxt3 = (TextView)this.findViewById(R.id.my_set_adresschoose_sheng_3); shiTxt3 = (TextView)this.findViewById(R.id.my_set_adresschoose_shi_3); topView3 = (TextView)this.findViewById(R.id.my_set_adresschoose_textview_3); 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; } shengAdapter = new ArrayAdapter(this,R.layout.my_set_addresschoose_listview_item,R.id.my_set_adresschoose_textview,sheng); shiAdapter = new ArrayAdapter(this,R.layout.my_set_addresschoose_listview_item,R.id.my_set_adresschoose_textview,shi); quAdapter = new ArrayAdapter(this,R.layout.my_set_addresschoose_listview_item,R.id.my_set_adresschoose_textview,qu); mListView1 = (ListView)this.findViewById(R.id.my_set_adresschoose_listview_1); mListView1.setAdapter(shengAdapter); mListView2 = (ListView)this.findViewById(R.id.my_set_adresschoose_listview_2); mListView2.setAdapter(shiAdapter); mListView3 = (ListView)this.findViewById(R.id.my_set_adresschoose_listview_3); mListView3.setAdapter(quAdapter); shengTxt2.setOnClickListener(click); shengTxt3.setOnClickListener(click); shiTxt3.setOnClickListener(click); 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(); } }); onBackLinear.setOnClickListener(click); } OnClickListener click = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.my_set_adresschoose_sheng_2: mLinearLayout1.setVisibility(View.VISIBLE); mLinearLayout2.setVisibility(View.GONE); mLinearLayout3.setVisibility(View.GONE); break; case R.id.my_set_adresschoose_sheng_3: mLinearLayout1.setVisibility(View.VISIBLE); mLinearLayout2.setVisibility(View.GONE); mLinearLayout3.setVisibility(View.GONE); break; case R.id.my_set_adresschoose_shi_3: mLinearLayout1.setVisibility(View.GONE); mLinearLayout2.setVisibility(View.VISIBLE); mLinearLayout3.setVisibility(View.GONE); break; case R.id.download_layout1: finish(); break; default: break; } } }; }
(三)这个是选择好地址之后的界面,主要还是界面的转换跟焦点的获取
有时候setFocusable(true)跟setFocusableInTouchMode(true)并不能使控件获得焦点,要再之前加上一句requestFocus(),这样控件就可以获得焦点.
public class BuyAddress extends Activity { private EditText jiequText; private EditText nameText; private EditText phoneText; private String provinces; private AddressInfo myAddress; private LinearLayout shengLinear; private LinearLayout jiequLinear; private LinearLayout nameLinear; private LinearLayout phoneLinear; private TextView shengText; private TextView jiequTextView; private TextView nameTextView; private TextView phoneTextView; private Button postBtn; private AddressInfo addressinfo; private CheckBox checkBox; private LinearLayout onBackLinear; private TextView topTxt; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.my_set_buyaddress); myAddress = new AddressInfo(); Intent i = getIntent(); provinces = i.getStringExtra("address"); Bundle b = i.getBundleExtra("address_id"); topTxt = (TextView) this.findViewById(R.id.txt_linear); onBackLinear = (LinearLayout) this.findViewById(R.id.download_layout1); topTxt.setText("添加收货地址"); shengText = (TextView) this.findViewById(R.id.my_set_buyaddress_sheng); jiequText = (EditText) this.findViewById(R.id.my_set_buyaddress_jiequ); nameText = (EditText) this.findViewById(R.id.my_set_buyaddress_name); phoneText = (EditText) this.findViewById(R.id.my_set_buyaddress_phone); shengLinear = (LinearLayout) this .findViewById(R.id.my_set_buyaddress_sheng_linear); jiequLinear = (LinearLayout) this .findViewById(R.id.my_set_buyaddress_jiequ_linear); nameLinear = (LinearLayout) this .findViewById(R.id.my_set_buyaddress_name_linear); phoneLinear = (LinearLayout) this .findViewById(R.id.my_set_buyaddress_phone_linear); jiequTextView = (TextView) this .findViewById(R.id.my_set_buyaddress_jiequ_text); nameTextView = (TextView) this .findViewById(R.id.my_set_buyaddress_name_text); phoneTextView = (TextView) this .findViewById(R.id.my_set_buyaddress_phone_text); checkBox = (CheckBox) this.findViewById(R.id.my_set_address_checkbox); postBtn = (Button) this .findViewById(R.id.my_set_buyaddress_address_btn); if (provinces == null) { } else { myAddress.setProvinces(provinces); shengText.setText(provinces); } shengLinear.setOnClickListener(click); jiequLinear.setOnClickListener(click); nameLinear.setOnClickListener(click); phoneLinear.setOnClickListener(click); postBtn.setOnClickListener(click); onBackLinear.setOnClickListener(click); jiequText.setOnFocusChangeListener(focusChanger); nameText.setOnFocusChangeListener(focusChanger); phoneText.setOnFocusChangeListener(focusChanger); if (b != null) { addressinfo = (AddressInfo) b.get("address"); phoneText.setVisibility(View.GONE); phoneText.setText(addressinfo.getPhone()); phoneLinear.setVisibility(View.VISIBLE); phoneTextView.setText(addressinfo.getPhone()); shengText.setText(addressinfo.getProvinces()); jiequText.setText(addressinfo.getStreet()); nameText.setText(addressinfo.getName()); phoneText.setText(addressinfo.getPhone()); jiequText.setVisibility(View.GONE); jiequLinear.setVisibility(View.VISIBLE); jiequTextView.setText(addressinfo.getStreet()); nameText.setVisibility(View.GONE); nameLinear.setVisibility(View.VISIBLE); nameTextView.setText(addressinfo.getName()); if(addressinfo.isStatus()){ checkBox.setChecked(true); }else{ checkBox.setChecked(false); } myAddress.setId(addressinfo.getId()); myAddress.setProvinces(addressinfo.getProvinces()); myAddress.setStreet(addressinfo.getStreet()); myAddress.setName(addressinfo.getName()); myAddress.setPhone(addressinfo.getPhone()); myAddress.setStatus(addressinfo.isStatus()); } } 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