废话不多说,首先附上图,效果如下:
一。 我想实现这样的效果:
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) {
}
};
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangyupeng_hehe/archive/2010/08/30/5849998.aspx
分享到:
相关推荐
Android高级应用源码-Spinner多级联动.zip
Android高级应用源码-Spinner多级联动.rar
android实现多级菜单联动操作,利用ViewPager可以实现页面之间的滑动。
android 多级菜单经典demo.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android竖直滑动选择器,三级联动,二级联动.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android(三级联动)全国省市县下拉地址选择源码.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
一款通用的筛选组件,支持单选,复选,多级筛选,筛选联动,筛选数据懒加载
这是安卓spinner多级联动demo,实现省市区多级联动下拉框效果
Spinner多级联动.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android多级下拉列表 菜单 ,可向下选择3级子菜单.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android spinner 省市区三级连动源码.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android省市区三级联动滚轮选择组件,基于开源组件Android-wheel来实现,效果非常不错,可以直接拿去用
0依赖,只需引用一个js和css即可 样式可自己定制,也可使用默认样式 一个页面同时实例化多个组件 jquery zepto vue 均适用 支持最多6级级联 ...适用于android和iOS设备(PC端支持IE9+,不过PC端上滑动体验不太实用)
类似淘宝手机客户端的,选择收货地址的三级联动滚动选择组件。好东西不多说,谁用谁知道~
通常我们在外卖或者商城app会看到竖排二级列表的界面,点击左边列表的分类菜单,右边列表则会显示对应分类下的列表内容,本demo提供简单的功能实现仅供参考。
Android高仿美团外卖点菜联动列表效果.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android 省市区三级联动(真实数据).rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
各种地址选择器 三级联动 亲测可用
在Android开发中使用Gallery实现“多级联动”(An-Beer工作室)
从真实项目中提取出来的,Android省市区三级联动滚轮选择组件,基于开源组件Android-wheel来实现,效果非常不错,可以直接拿去用.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考...