城市三级联动
程序员文章站
2024-03-14 20:28:34
...
今天给大家分享一个非常牛叉第三方的地区选择 三分钟所有代码搞定
效果如下:
废话不多讲 直接上代码
//3.0依赖
compile 'liji.library.dev:citypickerview:1.1.0'
//3.1.2 依赖
implementation'liji.library.dev:citypickerview:1.1.0'
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_gravity="center"
- android:layout_height="match_parent"
- tools:context="com.examsdfadple.fadministrator.sanjichengshiliandong.MainActivity">
- <TextView
- android:id="@+id/adresss"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
- package com.examsdfadple.fadministrator.sanjichengshiliandong;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.TextView;
- import com.lljjcoder.citypickerview.widget.CityPicker;
- public class MainActivity extends AppCompatActivity {
- private CityPicker cityPicker;
- private TextView adresss;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- public void initCityPicker() {
- //滚轮文字的大小
- //滚轮文字的颜色
- //省份滚轮是否循环显示
- //城市滚轮是否循环显示
- //地区(县)滚轮是否循环显示
- //滚轮显示的item个数
- //滚轮item间距
- cityPicker = new CityPicker.Builder(MainActivity.this)
- .textSize(20)//滚轮文字的大小
- .title("地址选择")
- .backgroundPop(0xa0000000)
- .titleBackgroundColor("#0CB6CA")
- .titleTextColor("#000000")
- .backgroundPop(0xa0000000)
- .confirTextColor("#000000")
- .cancelTextColor("#000000")
- .province("xx省")
- .city("xx市")
- .district("xx区")
- .textColor(Color.parseColor("#000000"))//滚轮文字的颜色
- .provinceCyclic(true)//省份滚轮是否循环显示
- .cityCyclic(false)//城市滚轮是否循环显示
- .districtCyclic(false)//地区(县)滚轮是否循环显示
- .visibleItemsCount(7)//滚轮显示的item个数
- .itemPadding(10)//滚轮item间距
- .onlyShowProvinceAndCity(false)
- .build();
- //监听方法,获取选择结果
- cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
- @Override
- public void onSelected(String... citySelected) {
- //省份
- String province = citySelected[0];
- //城市
- String city = citySelected[1];
- //区县(如果设定了两级联动,那么该项返回空)
- String district = citySelected[2];
- //邮编
- String code = citySelected[3];
- adresss.setText(province + city + district);
- Log.e("aaaaaaaaaaaaaa",adresss.getText().toString());
- }
- @Override
- public void onCancel() {
- }
- });
- }
- //隐藏软键盘
- // InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- private void initView() {
- adresss = (TextView) findViewById(R.id.adresss);
- adresss.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- initCityPicker();
- cityPicker.show();
- }
- });
- }
- // imm.hideSoftInputFromWindow(view.getWindowToken(),0); //强制隐藏键盘
- }
上一篇: JAVA(JDK)动态代理和Cglib代理的实现原理
下一篇: 省市县三级联动