有關android中多級聯動問題的解決

來源:互聯網
上載者:User

    廢話不多說,首先附,效果如下:

                                               

  一。 我想實現這樣的效果:

 

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

  }
 };
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.