Android中動態添加╱刪除的Spinner菜單

來源:互聯網
上載者:User

實現步驟:

 

第一步:建立Android 工程:SpinnerDemo。

                                                                   

第二步:編寫Activity 的子類別:SpinnerDemo,其程式碼如下:

 

package com.a3gs.spinner;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.*;

 

public class SpinnerDemo extends Activity {

    private TextView myTV;

    private Spinner mySp;

    private EditText myET;

    private Button addBtn, delBtn;

    private final String[] items = {"北京市", "上海市", "天津市", "福州市"} ;

    private ArrayAdapter<String> adapter;

    private List<String> allItems;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        myTV = (TextView) findViewById(R.id.myTV);

        myET = (EditText) findViewById(R.id.myET);

        addBtn = (Button) findViewById(R.id.addBtn);

        delBtn = (Button) findViewById(R.id.delBtn);

        mySp = (Spinner) findViewById(R.id.mySpinner);

        mySp.setVisibility(View.VISIBLE);

        allItems = new ArrayList<String>();

        for(int i=0; i < items.length; i++){

            allItems.add(items[i]);

        }

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allItems);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mySp.setAdapter(adapter);

        mySp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

           @Override

           public void onItemSelected(AdapterView<?> arg0, View arg1,

                  int arg2, long arg3) {

              // TODO Auto-generated method stub

              myTV.setText("您選擇的是:" + mySp.getSelectedItem().toString());

           }

           @Override

           public void onNothingSelected(AdapterView<?> arg0) {

              // TODO Auto-generated method stub

           }          

        });

       

        addBtn.setOnClickListener(new Button.OnClickListener(){

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              String ETText = myET.getText().toString();

              int len = adapter.getCount();

              // 檢查所添加的是否已經存在

              for(int i = 0; i < len; i++){

                  if(ETText.equals(adapter.getItem(i))){

                     return;

                  }

              }

             

              if(!ETText.equals("")){

                  adapter.add(ETText);

                  int position = adapter.getPosition(ETText);

                  mySp.setSelection(position);

                  myET.setText("");

              }

           }          

        });

       

        delBtn.setOnClickListener(new Button.OnClickListener(){

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              if(mySp.getSelectedItem() !=  null) {

                  adapter.remove(mySp.getSelectedItem().toString());

                  myET.setText("");

              }

              if(adapter.getCount() == 0){

                  myET.setText("");

              }

           }

        });

    }

}

 

第三步:修改res/layout/main.xml,其代碼如下:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:id="@+id/myTV"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

<EditText 

    android:id="@+id/myET"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    >

    <Button

       android:id="@+id/addBtn"

        android:layout_width="160sp"

        android:layout_height="wrap_content"

        android:text="@string/btn_text1"

        />

    <Button

       android:id="@+id/delBtn"

        android:layout_width="160sp"

        android:layout_height="wrap_content"

        android:text="@string/btn_text2"

        />

</LinearLayout>

<Spinner

    android:id="@+id/mySpinner"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    />

</LinearLayout>

第四步:修改res/layout/spinner_dropdown.xml,其代碼如下:

 

<?xml version="1.0" encoding="utf-8"?>

<TextView 

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/text"

    android:layout_width="wrap_content"

    android:layout_height="25sp"

    android:singleLine="true"

    style="?android:attr/spinnerDropDownItemStyle"

    />

 

擴充學習

 

setDropDownViewResource 主要是設定User 點擊Spinner 後出現的下拉式功能表樣式,除了前一個範例使用自設方式改變TextView 內容之外,android 亦提供兩種基本的樣式:

 android.R.layout.simple_spinner_item:TextView 的下拉式功能表。

 android.R.layout.simple_spinner_dropdown_item:除了有TextView,右邊有radio 的下拉式功能表。

 

查看 Android 原始碼中的simple_spinner_drop, , down_item.xml,內容如下:

<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="?android:attr/listPreferredItemHeight"

android:singleLine="true"

style="?android:attr/spinnerDropDownItemStyle"

/>

以下為自訂修改後,適用於spinner 的Layout:

<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="12sp"

android:singleLine="true"

style="?android:attr/spinnerDropDownItemStyle"

android:textSize="10sp"

/>

 

 

例子

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.