有关android中多级联动问题的解决

    废话不多说,首先附上图,效果如下:

                                                效果图

  一。 我想实现这样的效果:

 

                   1.当在第一个spinner里选择一个省份的时候,第二个spinner和edittext都会同步进行改变,而且两者的值都该相同;

                   2.当在第二个spinner里进行选择的时候,edittext的值也会随之改变,而且两者的值也该相同。

  二。我的困惑:

 

        对于spinner的二级联动,我好实现,但是,对于当两个spinner的选项改变时,edittext的值该如何改变,一时

  间,我确实有些闷了,从网上找了好些资料,没找到解决的办法。但我还是坚持,最后一个网友提醒了我, 它给我提供了思路,让我找到了

  解决问题的办法,在这里,再次感谢他!

  三。解决办法和代码(这里我只把主要的代码贴出来):

    1 )布局文件的代码,也就是我们看到的上面这个图的布局代码:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView android:id="@+id/weather_query"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
 >
    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1.0">
        <LinearLayout
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content">
            <TextView  android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="省份" />
            <Spinner 
                      android:id="@+id/spiner01"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1.0" />
        </LinearLayout>
        <LinearLayout   android:layout_width="fill_parent"
                        android:layout_height="wrap_content">
            <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="城市" />
            <Spinner
                     android:id="@+id/spiner02"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
    <FrameLayout android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_weight="1.0">
        <LinearLayout  android:layout_width="fill_parent"
                       android:layout_height="wrap_content">
             <EditText 
                       android:id="@+id/edittext"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:hint="中文或全拼(如:山东或shandong)"
                       android:singleLine="true"
                       android:layout_weight="1.0" />
                      
            <Button   
                       android:id="@+id/query_button"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="@string/ok" />
        </LinearLayout>
    </FrameLayout>

 <TextView android:textColor="#ffff0000"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
    <LinearLayout android:layout_width="wrap_content"
                 android:layout_height="10.0dip" />
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="-20.0px"
    android:text="@string/tips_1" />
</LinearLayout>
</ScrollView>        

 

 2)activity的代码:

package com.test.pp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class WeatherCityList extends Activity {

 private Spinner prov_spr;
 private Spinner city_spr;
 ArrayAdapter<String> adapter01;
 ArrayAdapter<String> adapter02;
 private String[][] arrayOfString1 = ConstData.city;
  private EditText edittext;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.weather_city_list);
  String[] arrayOfString = ConstData.province;
  adapter01 = new ArrayAdapter<String>(this,
  android.R.layout.simple_spinner_item, arrayOfString);
  adapter01.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  prov_spr = (Spinner) this.findViewById(R.id.spiner01);
  prov_spr.setAdapter(adapter01);
  prov_spr.setOnItemSelectedListener(selectListener);
  city_spr = (Spinner) this.findViewById(R.id.spiner02);
  city_spr.setAdapter(adapter02);
  edittext=(EditText) findViewById(R.id.edittext);
  city_spr.setOnItemSelectedListener(selectListener01);//用来处理效果二的代码以及监听事件
  }

 private OnItemSelectedListener selectListener = new OnItemSelectedListener() {

  @Override
  public void onItemSelected(AdapterView<?> parent, View v, int position,
    long id) {
   int pos = prov_spr.getSelectedItemPosition();
   adapter02 = new ArrayAdapter<String>(WeatherCityList.this,
   android.R.layout.simple_spinner_item, arrayOfString1[pos]);
   city_spr.setAdapter(adapter02);
   edittext.setText(city_spr.getSelectedItem().toString());//这行代码就是用来实现edittext里的值和前面两个spinner的值同步,把我们获取的城市的值写进edittext里
  }
  
  @Override
  public void onNothingSelected(AdapterView<?> arg0) {

  }
 };
 private OnItemSelectedListener selectListener01= new OnItemSelectedListener() {

  @Override
  public void onItemSelected(AdapterView<?> parent, View v, int position,
    long id) {
   edittext.setText(city_spr.getSelectedItem().toString());//实现效果二中当我们进行选择的时候,edittext里的值也进行改变
  }
  
  @Override
  public void onNothingSelected(AdapterView<?> arg0) {

  }
 };
 
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中实现下拉框多级联,可以通过以下步骤来完成: 1. 创建一个布局文件,用于显示下拉框和多级联的控件。 2. 在 Activity 中创建一个 ArrayList 对象,用于存储下拉框中的数据。 3. 在 Activity 中创建一个 ArrayAdapter 对象,用于将数据绑定到下拉框中。 4. 使用 Spinner 控件来显示下拉框,并设置 OnItemSelectedListener 监听器。 5. 在 OnItemSelectedListener 监听器中获取用户选择的数据,并更新下一级下拉框的数据。 6. 重复步骤 3-5,直到所有下拉框都被填充为止。 下面是一个简单的示例代码: ``` // 定义省市区的数据 private ArrayList<String> provinceList; private ArrayList<String> cityList; private ArrayList<String> areaList; // 定义下拉框控件 private Spinner spProvince; private Spinner spCity; private Spinner spArea; // 定义适配器对象 private ArrayAdapter<String> provinceAdapter; private ArrayAdapter<String> cityAdapter; private ArrayAdapter<String> areaAdapter; // 初始化数据 private void initData() { // 初始化省份数据 provinceList = new ArrayList<String>(); provinceList.add("北京"); provinceList.add("上海"); provinceList.add("广东"); // 初始化城市数据 cityList = new ArrayList<String>(); cityList.add("北京市"); cityList.add("上海市"); cityList.add("广州市"); cityList.add("深圳市"); // 初始化地区数据 areaList = new ArrayList<String>(); areaList.add("东城区"); areaList.add("朝阳区"); areaList.add("黄浦区"); areaList.add("徐汇区"); } // 初始化控件 private void initView() { // 获取控件对象 spProvince = (Spinner) findViewById(R.id.sp_province); spCity = (Spinner) findViewById(R.id.sp_city); spArea = (Spinner) findViewById(R.id.sp_area); // 创建适配器对象 provinceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, provinceList); cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityList); areaAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, areaList); // 设置下拉框样式 provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 绑定适配器到控件 spProvince.setAdapter(provinceAdapter); spCity.setAdapter(cityAdapter); spArea.setAdapter(areaAdapter); // 设置监听器 spProvince.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 获取用户选择的省份 String province = provinceList.get(position); // 根据省份更新城市数据 updateCityData(province); // 更新下一级下拉框的数据 updateAreaData(cityList.get(0)); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); spCity.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 获取用户选择的城市 String city = cityList.get(position); // 根据城市更新地区数据 updateAreaData(city); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } // 更新城市数据 private void updateCityData(String province) { // 根据省份获取城市数据 // 这里省略获取城市数据的代码 // 更新城市数据 cityAdapter.clear(); cityAdapter.addAll(cityList); cityAdapter.notifyDataSetChanged(); } // 更新地区数据 private void updateAreaData(String city) { // 根据城市获取地区数据 // 这里省略获取地区数据的代码 // 更新地区数据 areaAdapter.clear(); areaAdapter.addAll(areaList); areaAdapter.notifyDataSetChanged(); } ``` 在这个示例中,我们创建了三个下拉框控件,分别用于显示省份、城市和地区的数据。当用户选择省份时,我们会根据省份更新城市数据,并将第二个下拉框填充为新的城市数据。当用户选择城市时,我们会根据城市更新地区数据,并将第三个下拉框填充为新的地区数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值