Android 三級聯(lián)動 省市區(qū)選擇器
項(xiàng)目地址:https://gitee.com/hbhbh/area-selector
效果圖:
image.png
導(dǎo)入依賴
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()獲取選擇市區(qū)的編碼柒啤,
通過myAreaView.getCityName(" ")獲取選擇的省市區(qū)名稱涩堤,方法傳參為省市區(qū)之間的分隔符
例如myAreaView.getCityName("—"),返回值為 廣東省—深圳市—寶安區(qū)
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);
//如果調(diào)用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();