演讲: 我是个程序员,一天我坐在路边一边喝水一边苦苦检查bug。
这时一个乞丐在我边上坐下了,開始要饭,我认为可怜。就给了他1块钱。 然后接着调试程序。他可能生意不好,就无聊的看看我在干什么。然后过了一会。他缓缓地指着我的屏幕说,这里少了个分号…
谁说咱们程序员不能写出既幽默又能懂的博客呢,本人想推出一系列博文,能够给刚接触Android开发的做一个參考,也能够与接触Android已久的各路大神比較一下,本人喜欢交流,假设有写得不好的地方,欢迎大家指出.假设有更好的效果或者功能,希望大家多多指点,互相学习能够共同进步.
说了这么多,如今进入今天的猪蹄,推出<<暴走漫画系列--高仿淘宝收货地址>>,有网购经验的同学应该对这个非常熟悉,在淘宝的Androidclient里面,有一个功能是设置收货地址,不清楚的同学能够自己下载个淘宝client,自己加入一下看看.
本人一向喜欢贴图,这样有图有真相,才干说服每一个人,废话不多说先上自己程序启动画面,然后执行界面,最后一张是淘宝client对比图.
图片震楼 启动界面
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hpbGxheF9saQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="180" height="300" alt="" style="font-size:24px">执行界面
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hpbGxheF9saQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="180" height="300" alt="" style="font-size:24px">淘宝界面
淘宝client无法录屏所以这里仅仅能上静态的,大家能够自己打开淘宝client看看.
以下进行具体的代码解说:
(一)本人在程序启动的时候开了一条后台服务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();
}
});
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hpbGxheF9saQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="196" height="392" alt="">
(四)这个是选择好地址之后的界面,主要还是界面的转换跟焦点的获取
有时候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;
}
}
};
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hpbGxheF9saQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="195" height="392" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hpbGxheF9saQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="195" height="392" alt="">
源代码地址: http://down.51cto.com/data/1470577 (CSDN的上传了,可是不知道怎么的又没了,仅仅能用51CTO的)
尊重原创,转载请注明出处:http://blog.csdn.net/chillax_li/article/details/32936187
版权声明:本文博客原创文章,博客,未经同意,不得转载。