You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin Android中实现国家-州-城市联动输入框

Kotlin Android 实现国家-州-城市联动选择的思路

首先建议不要用EditText做选择控件,EditText是用于手动输入的,选择层级数据更适合用Spinner(下拉选择框)或者AutoCompleteTextView(带提示的输入框),下面是具体实现步骤:

1. 准备层级数据源

先定义数据类来存储国家、州、城市的关联关系:

// 城市数据类
data class City(val name: String)

// 州数据类,包含所属国家和下属城市
data class State(val name: String, val country: String, val cities: List<City>)

// 国家数据类
data class Country(val name: String)

然后初始化模拟数据源(实际项目可以从本地JSON或接口获取):

val sampleCountries = listOf(
    Country("中国"),
    Country("美国"),
    Country("日本")
)

val sampleStates = listOf(
    State("北京", "中国", listOf(City("朝阳区"), City("海淀区"))),
    State("加利福尼亚州", "美国", listOf(City("洛杉矶"), City("旧金山"))),
    State("东京都", "日本", listOf(City("新宿区"), City("涩谷区")))
)

2. 布局文件配置

在XML布局中添加三个Spinner(替换原来的EditText):

<Spinner
    android:id="@+id/spinner_country"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="选择国家"/>

<Spinner
    android:id="@+id/spinner_state"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="选择州/省"
    android:enabled="false"/> <!-- 默认禁用,选完国家再启用 -->

<Spinner
    android:id="@+id/spinner_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="选择城市"
    android:enabled="false"/>

3. 实现联动逻辑

在Activity/Fragment中初始化控件并设置选择监听:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val countrySpinner = findViewById<Spinner>(R.id.spinner_country)
        val stateSpinner = findViewById<Spinner>(R.id.spinner_state)
        val citySpinner = findViewById<Spinner>(R.id.spinner_city)

        // 设置国家Spinner的适配器
        countrySpinner.adapter = ArrayAdapter(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            sampleCountries.map { it.name }
        )

        // 国家选择监听
        countrySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val selectedCountry = sampleCountries[position].name
                // 过滤出当前国家对应的州
                val filteredStates = sampleStates.filter { it.country == selectedCountry }
                
                // 更新州Spinner的适配器并启用
                stateSpinner.adapter = ArrayAdapter(
                    this@MainActivity,
                    android.R.layout.simple_spinner_dropdown_item,
                    filteredStates.map { it.name }
                )
                stateSpinner.isEnabled = true
                // 重置城市Spinner
                citySpinner.isEnabled = false
                citySpinner.adapter = null
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }

        // 州选择监听
        stateSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val selectedStateName = parent?.selectedItem.toString()
                // 找到选中的州对应的城市
                val selectedState = sampleStates.find { it.name == selectedStateName }
                selectedState?.let {
                    // 更新城市Spinner的适配器并启用
                    citySpinner.adapter = ArrayAdapter(
                        this@MainActivity,
                        android.R.layout.simple_spinner_dropdown_item,
                        it.cities.map { city -> city.name }
                    )
                    citySpinner.isEnabled = true
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }
    }
}

4. 扩展优化建议

  • 如果数据量较大,建议用RecyclerView结合弹窗实现选择,避免Spinner下拉列表过长
  • 实际项目中可以把数据源放在本地JSON文件,通过Gson解析,或者从后端接口获取动态数据
  • 可以添加加载状态,比如从接口获取数据时显示ProgressBar

内容的提问来源于stack exchange,提问作者patrick mwanza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 16:00:16