`
muyu114
  • 浏览: 129774 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

有关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) {

}
};

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangyupeng_hehe/archive/2010/08/30/5849998.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics