模仿手机淘宝客户端新增地址中的地区选择
2016-08-26 09:32
701 查看
最近在做一个地区选择的项目,找了很多都不是很理想,昨天发现淘宝手机客户端新增地址地区选择很不错,我就简单的模仿了一下,效果自己可以下载个看看,好了不多说了,下面说代码,这里面用到的关键就是startActivityForResult这个方法。
首先我们要做一个TextView或者button来点击触发地区选择,我是分3个activity 来实现的,分别显示省,市,区三个级别,只有每层用startActivityForResult的方法返回数据,下面贴出代码省的Activity
三个省市区的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="0.1dip"
android:background="@color/line" />
<ListView
android:id="@+id/list_diqusheng"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:divider="@color/seedbar_bg"
android:listSelector="@android:color/transparent" >
</ListView>
</LinearLayout>
</LinearLayout>
自己看看效果就知道我如何布局了,应该很好懂,我不需要讲太多了,另为那些数据我是从服务器获取的,一层一层用LIST适配的,adatper就不贴了比较简单,只要是功能实现,基本是实现了,大家可以去看看效果。
首先我们要做一个TextView或者button来点击触发地区选择,我是分3个activity 来实现的,分别显示省,市,区三个级别,只有每层用startActivityForResult的方法返回数据,下面贴出代码省的Activity
package com.example.sqgl.boat; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import com.example.gkdemo.adapter.DiqulistAdapter; import com.example.sqgl.R; import com.example.sqgl.util.AlertKit; import com.example.sqgl.util.ProgressDialogUtils; import com.example.sqgl.util.TcGlobalConstant; import com.example.sqgl.util.TcJsonUtils; import com.example.sqgl.util.TcRegisRunnable; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class DiquActivity extends Activity{ private ListView list_diqusheng; private String[] sheng; private String[] shengid; private static final int DIQU = 122345; private static final int HJD = 122346; private static final int XZD = 122347; private static final int DYXZ = 342343; private Bundle bundle; private String id,name,name2,name3,type; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_diqu); bundle = this.getIntent().getExtras(); type = bundle.getString("type"); list_diqusheng=(ListView) findViewById(R.id. list_diqusheng); getdata2(); } private void getdata2() { // TODO Auto-generated method stub // ProgressDialogUtils.createProgressDialog(this); new Thread(new TcRegisRunnable(handler, TcGlobalConstant.Result.addren)).start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { ProgressDialogUtils.closeProgressDialog(); if (msg.what == TcGlobalConstant.Result.failed) { AlertKit.showAlertDialog(DiquActivity.this, TcGlobalConstant.Result.progresstitle, TcGlobalConstant.Result.failed_msg, TcGlobalConstant.Result.progress_ok, null); return; } switch (msg.what) { case TcGlobalConstant.Result.addren: try { JSONObject obj = (JSONObject) msg.obj; String string = obj.getString("result"); String content = obj.getString("msg"); if (string.equals("true")) { final List<String[]> loginstring = TcJsonUtils .addren(obj.getJSONObject("data")); sheng=loginstring.get(14); shengid=loginstring.get(15); DiqulistAdapter adapter=new DiqulistAdapter(DiquActivity.this, sheng); list_diqusheng.setAdapter(adapter); list_diqusheng.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent intent = new Intent( DiquActivity.this, DiqushiActivity.class); Bundle bundle = new Bundle(); bundle.putString("id",shengid[arg2]); bundle.putString("name",sheng[arg2]); bundle.putString("type",type); intent.putExtras(bundle); if (type.equals("1")) { startActivityForResult(intent, DIQU); } } }); } else { Log.i("消息阅读", "c失败"); ProgressDialogUtils.closeProgressDialog(); Toast.makeText(getApplicationContext(), "日志调取出错", 1) .show(); } } catch (JSONException e) { Log.d("消息阅读", "失败"); } break; default: break; } } }; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { android.util.Log.e("onTabActivityResult", "onActivityResult"); Log.v("resultCode", resultCode + ""); Log.v("requestCode", requestCode + ""); switch (requestCode) { case DIQU: if (resultCode==RESULT_OK) { bundle =data.getExtras(); id = bundle.getString("id"); name = bundle.getString("sheng"); name2 = bundle.getString("shi"); name3 = bundle.getString("xian"); Intent intent = new Intent(); intent.putExtra("id",id); intent.putExtra("sheng",name); intent.putExtra("shi",name2); intent.putExtra("xian",name3); // Bundle bundle = new Bundle(); // // bundle.putString("id",xianid2[arg2]); // bundle.putString("sheng",name2); // bundle.putString("shi",name); // bundle.putString("xian",xian[arg2]); // intent.putExtras(bundle); setResult(RESULT_OK, intent); DiquActivity.this. finish(); } break; } } }下面是市的代码
package com.example.sqgl.boat; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import com.example.gkdemo.adapter.DiqulistAdapter; import com.example.sqgl.R; import com.example.sqgl.util.AlertKit; import com.example.sqgl.util.Bimp; import com.example.sqgl.util.ProgressDialogUtils; import com.example.sqgl.util.TcGlobalConstant; import com.example.sqgl.util.TcJsonUtils; import com.example.sqgl.util.TcRegisRunnable; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class DiqushiActivity extends Activity{ private ListView list_diqusheng; private String[] shi; private String[] shiid; private Bundle bundle; private String id,name,name2,name3,type; private TextView tv_diqushi; private static final int DIQU = 122345; private static final int HJD = 122346; private static final int XZD = 122347; private static final int DYXZ = 342343; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_diqushi); bundle = this.getIntent().getExtras(); id = bundle.getString("id"); name = bundle.getString("name"); type = bundle.getString("type"); list_diqusheng=(ListView) findViewById(R.id. list_diqushi); tv_diqushi=(TextView) findViewById(R.id. tv_diqushi); tv_diqushi.setText(name.toString()); getdata3(id,"1"); tv_diqushi.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DiqushiActivity.this.finish(); } }); } private void getdata3(String id,String a) { // TODO Auto-generated method stub new Thread(new TcRegisRunnable(a,id,handler, TcGlobalConstant.Result.addshi)).start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { ProgressDialogUtils.closeProgressDialog(); if (msg.what == TcGlobalConstant.Result.failed) { AlertKit.showAlertDialog(DiqushiActivity.this, TcGlobalConstant.Result.progresstitle, TcGlobalConstant.Result.failed_msg, TcGlobalConstant.Result.progress_ok, null); return; } switch (msg.what) { case TcGlobalConstant.Result.addshi: try { JSONObject obj = (JSONObject) msg.obj; String string = obj.getString("result"); String content = obj.getString("msg"); if (string.equals("true")) { if (content.equals("1")) { final List<String[]> loginstring = TcJsonUtils .addshi(obj.getJSONObject("data")); shi=loginstring.get(0); shiid=loginstring.get(1); } DiqulistAdapter adapter=new DiqulistAdapter(DiqushiActivity.this, shi); list_diqusheng.setAdapter(adapter); list_diqusheng.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent intent = new Intent( DiqushiActivity.this, DiququActivity.class); Bundle bundle = new Bundle(); bundle.putString("id",shiid[arg2]); bundle.putString("name",shi[arg2]); bundle.putString("name2",name); intent.putExtras(bundle); if (type.equals("1")) { startActivityForResult(intent, DIQU); } } }); } else { Log.i("消息阅读", "c失败"); ProgressDialogUtils.closeProgressDialog(); Toast.makeText(getApplicationContext(), "日志调取出错", 1) .show(); } } catch (JSONException e) { Log.d("消息阅读", "失败"); } break; default: break; } } }; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { android.util.Log.e("onTabActivityResult", "onActivityResult"); Log.v("resultCode", resultCode + ""); Log.v("requestCode", requestCode + ""); switch (requestCode) { case DIQU: System.out.println("21412412"); if (resultCode==RESULT_OK) { bundle =data.getExtras(); id = bundle.getString("id"); name = bundle.getString("sheng"); name2 = bundle.getString("shi"); name3 = bundle.getString("xian"); Int c57a ent intent = new Intent(); intent.putExtra("id",id); intent.putExtra("sheng",name); intent.putExtra("shi",name2); intent.putExtra("xian",name3); // Bundle bundle = new Bundle(); // // bundle.putString("id",xianid2[arg2]); // bundle.putString("sheng",name2); // bundle.putString("shi",name); // bundle.putString("xian",xian[arg2]); // intent.putExtras(bundle); setResult(RESULT_OK, intent); DiqushiActivity.this. finish(); } break; } } }下面是区来返回结果
package com.example.sqgl.boat; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import com.example.gkdemo.adapter.DiqulistAdapter; import com.example.sqgl.R; import com.example.sqgl.util.AlertKit; import com.example.sqgl.util.ProgressDialogUtils; import com.example.sqgl.util.TcGlobalConstant; import com.example.sqgl.util.TcJsonUtils; import com.example.sqgl.util.TcRegisRunnable; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class DiququActivity extends Activity{ private ListView list_diququ; private String[] xian; private Bundle bundle; private String[] xianid,xianid2; private String id,name,name2,type; private TextView tv_diququ,tv_diququ2; private static final int DIQU = 122345; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_diququ); bundle = this.getIntent().getExtras(); id = bundle.getString("id"); name = bundle.getString("name"); name2 = bundle.getString("name2"); list_diququ=(ListView) findViewById(R.id. list_diququ); tv_diququ=(TextView) findViewById(R.id. tv_diququ); tv_diququ2=(TextView) findViewById(R.id. tv_diququ2); tv_diququ.setText(name2); tv_diququ2.setText(name); getdata3(id,"2"); tv_diququ.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DiququActivity.this.finish(); } }); tv_diququ2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DiququActivity.this.finish(); } }); } private void getdata3(String id,String a) { // TODO Auto-generated method stub new Thread(new TcRegisRunnable(a,id,handler, TcGlobalConstant.Result.addshi)).start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { ProgressDialogUtils.closeProgressDialog(); if (msg.what == TcGlobalConstant.Result.failed) { AlertKit.showAlertDialog(DiququActivity.this, TcGlobalConstant.Result.progresstitle, TcGlobalConstant.Result.failed_msg, TcGlobalConstant.Result.progress_ok, null); return; } switch (msg.what) { case TcGlobalConstant.Result.addshi: try { JSONObject obj = (JSONObject) msg.obj; String string = obj.getString("result"); String content = obj.getString("msg"); if (string.equals("true")) { if (content.equals("2")) { final List<String[]> loginstring = TcJsonUtils .addshi(obj.getJSONObject("data")); xian=loginstring.get(0); xianid=loginstring.get(1); xianid2=loginstring.get(2); } DiqulistAdapter adapter=new DiqulistAdapter(DiququActivity.this, xian); list_diququ.setAdapter(adapter); list_diququ.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.putExtra("id",xianid2[arg2]); intent.putExtra("sheng",name2); intent.putExtra("shi",name); intent.putExtra("xian",xian[arg2]); // Bundle bundle = new Bundle(); // // bundle.putString("id",xianid2[arg2]); // bundle.putString("sheng",name2); // bundle.putString("shi",name); // bundle.putString("xian",xian[arg2]); // intent.putExtras(bundle); setResult(RESULT_OK, intent); DiququActivity.this. finish(); } }); } else { Log.i("消息阅读", "c失败"); ProgressDialogUtils.closeProgressDialog(); Toast.makeText(getApplicationContext(), "日志调取出错", 1) .show(); } } catch (JSONException e) { Log.d("消息阅读", "失败"); } break; default: break; } } }; }
三个省市区的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="0.1dip"
android:background="@color/line" />
<ListView
android:id="@+id/list_diqusheng"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:divider="@color/seedbar_bg"
android:listSelector="@android:color/transparent" >
</ListView>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@color/darkgray" > <TextView android:id="@+id/tv_diqushi" style="@style/contentBlack" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp" android:layout_gravity="center_vertical" android:text="身份证号" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#eeeeee" android:orientation="vertical" > <View android:layout_width="fill_parent" android:layout_height="0.1dip" android:background="@color/line" /> <ListView android:id="@+id/list_diqushi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" android:divider="@color/seedbar_bg" android:listSelector="@android:color/transparent" > </ListView> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@color/darkgray" > <TextView android:id="@+id/tv_diququ" style="@style/contentBlack" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp" android:layout_gravity="center_vertical" android:text="身份证号" /> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="0.1dip" android:background="@color/line" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:background="@color/darkgray" > <TextView android:id="@+id/tv_diququ2" style="@style/contentBlack" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp" android:layout_gravity="center_vertical" android:text="身份证号" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#eeeeee" android:orientation="vertical" > <View android:layout_width="fill_parent" android:layout_height="0.1dip" android:background="@color/line" /> <ListView android:id="@+id/list_diququ" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" android:divider="@color/seedbar_bg" android:listSelector="@android:color/transparent" > </ListView> </LinearLayout> </LinearLayout>
自己看看效果就知道我如何布局了,应该很好懂,我不需要讲太多了,另为那些数据我是从服务器获取的,一层一层用LIST适配的,adatper就不贴了比较简单,只要是功能实现,基本是实现了,大家可以去看看效果。
相关文章推荐
- git版本控制 学习之路(一)
- 手机验证码发送(ThinkPHP)
- 判断点是否在多边型内
- 文件分割
- 集成电路883和883b有什么区别
- 蓝鸥Unity开发基础——方法重载
- 集成电路883和883b有什么区别
- 集成电路883和883b有什么区别
- 腾讯编程
- 集成电路883和883b有什么区别
- js 禁止 复制,粘贴代码
- LeetCode: Top K Frequent Elements
- maven下载到配置到检测
- RNN 调参经验
- 版本更新
- servlet网页生成二维码
- Hash Join vs Nest Loop vs Sort Merge
- 关于grep的使用以及正则表达式
- Android Retrofit2&OkHttp3添加统一的请求头Header
- 二叉树模板