安卓端
开发工具:AndroidStudio
数据传输:okhttp3
后台:
开发工具:Eclipse
框架:SpringBoot
数据库:Mysql
功能简介:实现地区的多级联动选择,通过六个文本框的点击事件实现,也可根据自己需要调整事件方法
效果预览:(刚实现了功能,页面可能有点丑,大家见谅,样式可自己调整)
1,数据库准备:
必须字段:
id(唯一):id标识
pid:存储父级id
name:名称
例:
1 0 中国
2 1 山东省
3 1 北京市
4 2 济南市
(好理解吧)
2,前台
(点击文本框弹出单选列表,列表内容根据父级选项显示)
首先定义显示页面,这里只需要六个文本框即可
然后便是Activity,直接上代码
缺少依赖的可参照我的博文(AndroidStudio中依赖导入的方法)
Httputil工具类也在我的博文中有
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.jjyl.utils.HttpUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;
public class AreaTest extends AppCompatActivity {
private EditText sheng;
private EditText shi;
private EditText qu;
private EditText zhen;
private EditText shequ;
private EditText cun;
private String[] param;
private String length;
private int num=0;
public static final MediaType JSON=MediaType.parse("application/json; charset=utf-8");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area_test);
initview();
}
//初始化组件
public void initview(){
sheng = (EditText) findViewById(R.id.sheng); //省
shi = (EditText)findViewById(R.id.shi);//市
qu = (EditText)findViewById(R.id.qu);//区
zhen = (EditText)findViewById(R.id.zhen);//镇
shequ = (EditText)findViewById(R.id.shequ);//社区
cun = (EditText)findViewById(R.id.cun);//村
//设置输入框点击监听
//省监听
sheng.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
showSheng();
return false;
}
});
//市监听
shi.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
{
num = 1;
panduan(num);
}
return false;
}
});
qu.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
{
num = 2;
panduan(num);
}
return false;
}
});
zhen.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
{
num = 3;
panduan(num);
}
return false;
}
});
shequ.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
{
num = 4;
panduan(num);
}
return false;
}
});
cun.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
{
num = 5;
panduan(num);
}
return false;
}
});
}
//省份不需选择父级目录,点击即可选择
public void showSheng(){
HttpUtil.sendOkHttpRequest(MainActivity.URL + "sys/getpro", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String data = response.body().string();
System.out.println(data);
if (response.code() == 200) {
if(data.length()>2) {
runOnUiThread(new Runnable() {
@Override
public void run() {
param = data.split(",");
length = "" + param.length;
//去除符号
int l = Integer.parseInt(length);
if(l>1){
param[0] = param[0].substring(2, param[0].length() - 1);
param[l - 1] = param[l - 1].substring(1, param[l - 1].length() - 2);
for (int i = 1; i < l - 1; i++) {
param[i] = param[i].substring(1, param[i].length() - 1);
}}else{
param[0] = param[0].substring(2, param[0].length() - 2);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AreaTest.this);
builder.setIcon(com.example.jjyl.R.mipmap.ic_launcher);//图标
builder.setTitle("省份选择");
final String[] par = param;
builder.setItems(par, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AreaTest.this.sheng.setText(par[which]);
}
});
builder.show();
}
});
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}
});
}
//判断父级输入框是否已选择
public void panduan(int num){
String param2;
switch (num){
case 1:
param2= sheng.getText().toString();
if (param2.equals("")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AreaTest.this,"请先选择省份!",Toast.LENGTH_SHORT).show();
}
});
}else{
showArea(param2,num);
}
break;
case 2:
param2= shi.getText().toString();
if (param2.equals("")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AreaTest.this,"请先选择城市!",Toast.LENGTH_SHORT).show();
}
});
}else{
showArea(param2,num);
}
break;
case 3:
param2= qu.getText().toString();
if (param2.equals("")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AreaTest.this,"请先选择区!",Toast.LENGTH_SHORT).show();
}
});
}else{
showArea(param2,num);
}
break;
case 4:
param2= zhen.getText().toString();
if (param2.equals("")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AreaTest.this,"请先选择镇!",Toast.LENGTH_SHORT).show();
}
});
}else{
showArea(param2,num);
}
break;
case 5:
param2= shequ.getText().toString();
if (param2.equals("")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AreaTest.this,"请先选择社区!",Toast.LENGTH_SHORT).show();
}
});
}else{
showArea(param2,num);
}
break;
}
}
//根据父项显示子项列表
public void showArea(String par2, final int num){
JSONObject jsonParam=new JSONObject();
try {
jsonParam.put("pname",par2);
} catch (JSONException e) {
e.printStackTrace();
}
String json = jsonParam.toString();
RequestBody requestBody=RequestBody.create(JSON,json);
HttpUtil.sendOkHttpResponse(MainActivity.URL+"sys/getarea", requestBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String data = response.body().string();
System.out.println(data);
if (response.code() == 200) {
if(data.length()>2) {
runOnUiThread(new Runnable() {
@Override
public void run() {
param = data.split(",");
length = "" + param.length;
//去除符号
int l = Integer.parseInt(length);
if(l>1){
param[0] = param[0].substring(2, param[0].length() - 1);
param[l - 1] = param[l - 1].substring(1, param[l - 1].length() - 2);
for (int i = 1; i < l - 1; i++) {
param[i] = param[i].substring(1, param[i].length() - 1);
}}else{
param[0] = param[0].substring(2, param[0].length() - 2);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AreaTest.this);
builder.setIcon(com.example.jjyl.R.mipmap.ic_launcher);//图标
builder.setTitle("选择");
final String[] par = param;
builder.setItems(par, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//将选择的项目传递给输入框显示,如果值父项发生变化,子项清空
switch (num){
case 1:
AreaTest.this.shi.setText(par[which]);
AreaTest.this.qu.setText("");
AreaTest.this.zhen.setText("");
AreaTest.this.shequ.setText("");
AreaTest.this.cun.setText("");
break;
case 2:
AreaTest.this.qu.setText(par[which]);
AreaTest.this.zhen.setText("");
AreaTest.this.shequ.setText("");
AreaTest.this.cun.setText("");
break;
case 3:
AreaTest.this.zhen.setText(par[which]);
AreaTest.this.shequ.setText("");
AreaTest.this.cun.setText("");
break;
case 4:
AreaTest.this.shequ.setText(par[which]);
AreaTest.this.cun.setText("");
break;
case 5:
AreaTest.this.cun.setText(par[which]);
break;
}
}
});
builder.show();
}
});
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}
});
}
}
2,后台代码
Mapper:
//直接获得省
@Select("SELECT NAME FROM 表名 WHERE pid =(SELECid FROM 表名 WHERE NAME = '中国')")
public List getPro();
//通过父级获取子项
@Select("SELECT NAME FROM 表名 WHERE pid =(SELECT id FROM 表名 WHERE NAME = #{pname})")
public List getArea(@Param("pname")String pname);
Service:
public List getPro();
public List getArea(String pname);
ServiceImpl:
public List getPro() {
return sysmapper.getPro();
}
public List getArea(String pname){
return sysmapper.getArea(pname);
}
Controller:
/**
* 获得省份
* @return pro
*/
//@PathVariable:用于获取url中的数据
@GetMapping(value = "sys/getpro")
public List getPro(){
try {
List pro =sysservice.getPro();
return pro;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
//获得下级区划
@RequestMapping(value = "sys/getarea",method = RequestMethod.POST)
public List getArea(@RequestBody Map param){
String pname =param.get("pname").toString();
try {
List pro =sysservice.getArea(pname);
return pro;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
安卓小白,代码有不合适的地方还请多多包涵。