欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用Spinner實現(xiàn)城市級聯(lián)下拉框

 更新時間:2017年12月27日 11:50:35   作者:Adan0520  
這篇文章主要為大家詳細介紹了Android使用Spinner實現(xiàn)城市級聯(lián)下拉框,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近寫一個使用Spinner實現(xiàn)城市級聯(lián)下拉框的Dome,現(xiàn)在總結一下,第一次寫博客,互相學習。

activity_main.xml里面有三個Spinner

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="@color/white" 
  android:orientation="horizontal" 
  tools:context=".MainActivity"> 
 
  <Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 
 
  <Spinner 
    android:id="@+id/spinner2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:visibility="invisible" /> 
 
  <Spinner 
    android:id="@+id/spinner3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:visibility="invisible" /> 
 
</LinearLayout> 

Spinner的每一個item布局,里面只有一個TextView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:background="@color/white" 
  android:gravity="center" 
  android:orientation="horizontal" 
  android:padding="5dp"> 
 
  <TextView 
    android:id="@+id/txt_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="名稱" 
    android:textSize="16dp" /> 
</LinearLayout> 

下面是SpinnerAdapter,一般會用到Adapter的有如下幾個控件

(1)列表視圖控件-ListView
(2)縮略圖瀏覽器控件-Gallery
(3)網(wǎng)格控件-GridView
(4)下拉列表控件-Spinner
(5)自動提示文本框-AutoCompleteTextView
(6)支持展開/收縮功能的列表控件-ExpandableListView
適配器的作用是用來處理數(shù)據(jù)并將數(shù)據(jù)綁定到AdapterView上,是AdapterView視圖與與數(shù)據(jù)之間的一個橋梁。

/** 
 * @author: xiaolijuan 
 * @description: Spinner適配器 
 * @projectName: SpinnerProject 
 * @date: 2015-10-18 
 * @time: 00:19 
 */ 
public class SpinnerAdapter extends BaseAdapter { 
  private Context context; 
  private String[] array; 
  private int layoutId; 
 
  /** 
   * 構造方法 
   * @param context 上下文對象 
   * @param array 數(shù)組 
   * @param layoutId 布局Id 
   */ 
  public SpinnerAdapter(Context context, String[] array, int layoutId) { 
    this.context = context; 
    this.array = array; 
    this.layoutId = layoutId; 
  } 
 
  /** 
   * 獲取Item總數(shù) 
   * @return 
   */ 
  @Override 
  public int getCount() { 
    return array.length; 
  } 
 
  /** 
   * 獲取一個Item對象 
   * @param position 
   * @return 
   */ 
  @Override 
  public Object getItem(int position) { 
    return array[position]; 
  } 
 
  /** 
   * 獲取指定item的ID 
   * @param position 
   * @return 
   */ 
  @Override 
  public long getItemId(int position) { 
    return position; 
  } 
 
  /** 
   * 繪制的內(nèi)容均在此實現(xiàn) 
   * @param position position就是位置從0開始 
   * @param convertView convertView是Spinner中每一項要顯示的view 
   * @param parent parent就是父窗體了,也就是Spinner 
   * @return 
   */ 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
    View item = convertView != null ? convertView : View.inflate(context, layoutId, null); 
    TextView txt_name = (TextView) item.findViewById(R.id.txt_name); 
    txt_name.setText(array[position]); 
    return item; 
  } 
} 

java 代碼:注釋寫的很清楚

/** 
 * 使用Spinner實現(xiàn)城市級聯(lián)下拉框 
 * Spinner最簡單使用方式步驟如下: 
 * 第一步:在布局文件中添加Spinner控件。 
 * 第二步:在Acitvity中通過id找到它。 
 * 第三步:給Spinner綁定一個適配器。 
 * 第四步:綁定監(jiān)聽器就可以用了。 
 */ 
public class MainActivity extends Activity { 
  private Spinner spinner1, spinner2, spinner3; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    spinner1 = (Spinner) findViewById(R.id.spinner1); 
    spinner2 = (Spinner) findViewById(R.id.spinner2); 
    spinner3 = (Spinner) findViewById(R.id.spinner3); 
 
    //加載省份列表 
    loadProvince(); 
    //設置spinner1的監(jiān)聽事件 
    spinner1.setOnItemSelectedListener(new Spinner1ClickListener()); 
    //加載城市列表 
    loadCity(); 
    //設置spinner2的監(jiān)聽事件 
    spinner2.setOnItemSelectedListener(new Spinner2ClickListener()); 
    //加載區(qū)域列表 
    loadGZArea(); 
    //設置spinner3的監(jiān)聽事件 
    spinner3.setOnItemSelectedListener(new Spinner3ClickListener()); 
  } 
 
  /** 
   * 加載省份列表 
   */ 
  public void loadProvince() { 
    String[] array1 = new String[]{"請選擇", "廣東省"}; 
    SpinnerAdapter adapterOne = new SpinnerAdapter(this, array1, R.layout.activity_item); 
    spinner1.setAdapter(adapterOne); 
  } 
 
  /** 
   * 加載城市列表 
   */ 
  public void loadCity() { 
    String[] array2 = new String[]{"請選擇", "廣州市", "深圳市"}; 
    SpinnerAdapter modelTwo = new SpinnerAdapter(this, array2, R.layout.activity_item); 
    spinner2.setAdapter(modelTwo); 
  } 
 
  /** 
   * 加載廣州區(qū)域列表 
   */ 
  public void loadGZArea() { 
    String[] array3 = new String[]{"請選擇", "天河區(qū)", "越秀區(qū)", "荔灣區(qū)", "海珠區(qū)", "蘿崗區(qū)", "白云區(qū)", "黃埔區(qū)", "花都區(qū)"}; 
    SpinnerAdapter modelThree = new SpinnerAdapter(this, array3, R.layout.activity_item); 
    spinner3.setAdapter(modelThree); 
  } 
 
  /** 
   * 加載深圳區(qū)域列表 
   */ 
  public void loadSZArea() { 
    String[] array3 = new String[]{"請選擇", "龍崗區(qū)", "南山區(qū)", "福田區(qū)", "羅湖區(qū)", "鹽田區(qū)", "寶安區(qū)"}; 
    SpinnerAdapter modelThree = new SpinnerAdapter(this, array3, R.layout.activity_item); 
    spinner3.setAdapter(modelThree); 
  } 
 
  /** 
   * Spinner1點擊事件 
   */ 
  public class Spinner1ClickListener implements AdapterView.OnItemSelectedListener { 
 
    @Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      String str = (String) adapterView.getItemAtPosition(i); 
      //判斷是否選擇城市,如果沒有選擇那么就隱藏Spinner2,Spinner3兩個下拉框,否則顯示Spinner2下拉框,繼續(xù)隱藏Spinner3 
      if (str.equals("請選擇")) { 
        spinner2.setVisibility(View.INVISIBLE); 
        spinner3.setVisibility(View.INVISIBLE); 
      } else { 
        spinner2.setVisibility(View.VISIBLE); 
 
        //將第二個下拉框的選項重新設置為選中“請選擇”這個選項。 
        spinner2.setSelection(0); 
      } 
 
      Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); 
    } 
 
    @Override 
    public void onNothingSelected(AdapterView<?> adapterView) { 
 
    } 
  } 
 
  /** 
   * Spinner2點擊事件 
   */ 
  public class Spinner2ClickListener implements AdapterView.OnItemSelectedListener { 
 
    @Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      String str = (String) adapterView.getItemAtPosition(i); 
      if (str.equals("請選擇")) { 
        spinner3.setVisibility(View.INVISIBLE); 
      } else { 
        //顯示第三個Spinner3 
        spinner3.setVisibility(View.VISIBLE); 
 
        if (str.equals("深圳市")) { 
          //重新加載深圳區(qū)域列表 
          loadSZArea(); 
        } else if (str.equals("廣州市")) { 
          //重新加載廣州區(qū)域列表 
          loadGZArea(); 
        } 
      } 
      Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); 
    } 
 
    @Override 
    public void onNothingSelected(AdapterView<?> adapterView) { 
 
    } 
  } 
 
  /** 
   * Spinner3點擊事件 
   */ 
  public class Spinner3ClickListener implements AdapterView.OnItemSelectedListener { 
 
    @Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      String str = (String) adapterView.getItemAtPosition(i); 
      Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); 
    } 
 
    @Override 
    public void onNothingSelected(AdapterView<?> adapterView) { 
 
    } 
  } 
} 

下面是布局的效果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android中如何安全地打印日志詳解

    Android中如何安全地打印日志詳解

    日志是我們在日常開發(fā)中必不可少的一部分,下面這篇文章主要給大家介紹了關于Android中如何安全地打印日志的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們一起來看看吧。
    2017-12-12
  • Android中注解處理器APT用法示例

    Android中注解處理器APT用法示例

    APT全稱Annotation Processing Tool,即注解處理器,APT是一種處理注釋的工具, 它對源代碼文件進行檢測找出其中的注解,并使用注解進行額外的處理,給我們自動生成代碼,簡化使用,很多流行框架都使用到了APT技術,如 ButterKnife,Retrofit,Arouter,EventBus 等等
    2023-12-12
  • 60條Android開發(fā)注意事項與經(jīng)驗總結

    60條Android開發(fā)注意事項與經(jīng)驗總結

    我們在Android App開發(fā)過程中總結了60條技術經(jīng)驗注意事項,大家在開發(fā)過程中一定要注意,下面我們來詳細說一下這60條經(jīng)驗
    2018-03-03
  • Android studio開發(fā)小型對話機器人app(實例代碼)

    Android studio開發(fā)小型對話機器人app(實例代碼)

    這篇文章主要介紹了Android studio開發(fā)一個小型對話機器人app,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Flutter開發(fā)之Widget自定義總結

    Flutter開發(fā)之Widget自定義總結

    這篇文章主要給大家介紹了關于Flutter開發(fā)中Widget自定義的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • Android仿微信雷達掃描效果的實現(xiàn)方法

    Android仿微信雷達掃描效果的實現(xiàn)方法

    最近看了一個視頻講了一種微信雷達掃描的實現(xiàn)方案,借鑒了一下,自己也寫一個玩玩,所以下面這篇文章主要給大家介紹了利用Android模仿微信雷達掃描效果的實現(xiàn)方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • Android監(jiān)聽Home鍵和Back鍵的區(qū)別介紹

    Android監(jiān)聽Home鍵和Back鍵的區(qū)別介紹

    這篇文章主要介紹了Android監(jiān)聽Home鍵和Back鍵的區(qū)別介紹,本文還同時給出了Home鍵監(jiān)聽的實現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • kotlin 協(xié)程上下文異常處理詳解

    kotlin 協(xié)程上下文異常處理詳解

    這篇文章主要為大家介紹了kotlin 協(xié)程上下文異常處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android 動畫之ScaleAnimation應用詳解

    Android 動畫之ScaleAnimation應用詳解

    本節(jié)講解ScaleAnimation 動畫在應用中的實現(xiàn),有需要的朋友可以參考下
    2012-12-12
  • Android EditText被軟鍵盤遮蓋的處理方法

    Android EditText被軟鍵盤遮蓋的處理方法

    android app新增了透明欄效果,結果發(fā)現(xiàn)鍵盤彈起后會遮蓋屏幕底部的EditText,沒有像想象中的調整窗口大小,并滾動ScrollView,將EditText顯示在鍵盤上方。下面小編把解決方法記錄一下,特此分享到腳本之家平臺,感興趣的朋友一起看看吧
    2016-10-10

最新評論