现在的位置: 首页 > 综合 > 正文

Android中动态添加╱删除的Spinner菜单

2013年08月04日 ⁄ 综合 ⁄ 共 5396字 ⁄ 字号 评论关闭

实现步骤:

 

第一步:建立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"

/>

 

 

例子效果图

 

抱歉!评论已关闭.