有关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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值