Android实现省市二级联动

省市资源文件:(放入res/values下)

链接:https://pan.baidu.com/s/1dMjY0m 密码:rf4w

Activity:

public class MainActivity extends Activity{
    protected TextView tv1;
    private PopupWindow popupWindow;
    private LinearLayout rootLl;
    private String[] provinces;
    private String TAG = this.getClass().getSimpleName();
    private RadioGroup radioGroup;
    private ArrayAdapter cityadapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        provinces = getResources().getStringArray(R.array.province);
        initView();
    }


    private void initView() {
        tv1 = (TextView) findViewById(R.id.tv1);
        rootLl = (LinearLayout) findViewById(R.id.root_ll);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopwindow();
            }
        });
    }
    //展示popWindow的方法
    private void showPopwindow() {
        if (null == popupWindow) {
            popupWindow = new PopupWindow(this);
            popupWindow.setWidth(rootLl.getWidth());
            popupWindow.setHeight((rootLl.getHeight() / 3) * 2);
            View popView = View.inflate(this, R.layout.popupwindow_layout, null);
            radioGroup = popView.findViewById(R.id.rg);
            final ListView listView = popView.findViewById(R.id.lv);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    String pro = provinces[checkedId];
                    Log.i(TAG, "onCheckedChanged: ***" + pro);
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.def,
                            R.layout.spinner_checked_text);
                    if (pro.equals("北京")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.北京,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("天津")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.天津,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("河北")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河北,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("山西")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山西,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("内蒙古")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.内蒙古,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("辽宁")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.辽宁,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("吉林")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.吉林,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("黑龙江")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.黑龙江,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("上海")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.上海,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("江苏")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江苏,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("浙江")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.浙江,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("安徽")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.安徽,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("福建")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.福建,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("江西")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江西,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("山东")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山东,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("河南")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河南,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("湖北")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖北,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("湖南")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖南,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("广东")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.广东,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("广西")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.广西,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("海南")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.海南,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("重庆")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.重庆,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("四川")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.四川,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("贵州")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.贵州,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("云南")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.云南,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("西藏")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.西藏,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("陕西")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.陕西,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("甘肃")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.甘肃,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("青海")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.青海,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("宁夏")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.宁夏,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("新疆")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.新疆,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("台湾")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.台湾,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("香港")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.香港,
                                R.layout.spinner_checked_text);
                    } else if (pro.equals("澳门")) {
                        cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.澳门,
                                R.layout.spinner_checked_text);
                    }

                    listView.setAdapter(cityadapter);
                }
            });
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String item = (String) cityadapter.getItem(position);
                    Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }
            });
            ViewGroup.LayoutParams layoutParams = radioGroup.getLayoutParams();
            layoutParams.width = RadioGroup.LayoutParams.MATCH_PARENT;
            //12dp---->转换成像素
            int dpValue = 12;
            float scale = getResources().getDisplayMetrics().density;
            int pxValue = (int) (dpValue * scale + 0.5f);
            int top = pxValue;
            int bottom = pxValue;
            for (int i = 0; i < provinces.length; i++) {
                RadioButton radioButton = new RadioButton(this);
                radioButton.setId(i);
                radioButton.setChecked(false);
                radioButton.setText(provinces[i]);
                radioButton.setTextColor(getResources().getColorStateList(R.color.colorAccent));
                radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
                radioButton.setBackgroundResource(R.drawable.bg_rb_d_sc);
                radioButton.setLayoutParams(layoutParams);
                radioButton.setPadding(0, top, 0, bottom);
                radioButton.setGravity(Gravity.CENTER);
                radioGroup.addView(radioButton);
            }
            popupWindow.setContentView(popView);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
            RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
            rb.setChecked(true);
        }
        popupWindow.showAsDropDown(tv1);
    }
}

主XML:

<LinearLayout 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_height="match_parent"
    android:id="@+id/root_ll"
    android:background="#FFFFFF"
    android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"
                    android:text="全国" />
        </LinearLayout>
</LinearLayout>

PopupWindow布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <ScrollView
        android:scrollbars="none"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></RadioGroup>
    </ScrollView>

    <ListView
        android:layout_marginLeft="20dp"
        android:id="@+id/lv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.5">
    </ListView>
</LinearLayout>


Spinner_Checked_layout布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    android:gravity="center"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</TextView>

Drawable文件:
背景文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_rb_d_single_checked" android:state_checked="true" />
    <item android:drawable="@drawable/bg_rb_d_sp_normal" />
</selector>

single drawble文件

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape>
            <solid android:color="@android:color/white" />
            <stroke
                android:width="10dp"
                android:color="#1E7FFF" />
        </shape>
    </item>
    <item android:left="5dp">
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>


normal drawble文件

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#F3F3F3" />
</shape>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值