Android 三级联动 省市区选择器
项目地址:https://gitee.com/hbhbh/area-selector
效果图:
导入依赖
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.gitee.hbha:area-selector:v1.4'
}
XML文件使用,直接加入控件
<g.hbh.areaselector.MyAreaView
android:id="@+id/myAreaView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在Activity绑定之后,可以通过myAreaView.getCityNo()获取选择市区的编码,
通过myAreaView.getCityName(" ")获取选择的省市区名称,方法传参为省市区之间的分隔符
例如myAreaView.getCityName("—"),返回值为 广东省—深圳市—宝安区
Activity里面使用:
Dialog dialog = new Dialog(MerchantBankActivity.this, R.style.edit_AlertDialog_style);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
//直接new一个控件即可
MyAreaView myAreaView = new MyAreaView(this);
//如果调用setButtonOnClickListener,传入一个Listener,布局就会多一个button
//不想要这个按钮的直接自定义,不要传这个Listener就好了
myAreaView.setButtonOnClickListener(v -> {
bankSubCityNo = myAreaView.getCityNo();
textView36.setText(myAreaView.getCityName(" "));
dialog.dismiss();
});
linearLayout.addView(myAreaView);
dialog.setContentView(linearLayout);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
Window w = dialog.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.width = 1000;
dialog.onWindowAttributesChanged(lp);
dialog.show();