Android UI設計 下拉式功能表Spinner用法 動態添加刪除Spinner功能表項目

來源:互聯網
上載者:User

Spinner是一種下接菜單,類似HTML中的select標籤,點擊後彈出一個對話方塊,顯示幾個供選擇的選項,手機螢幕大小有限,如果都用RadioGroup選項按鈕,會佔用很大的空間。今天的例子最終效果如:

Spinner需要綁定一個適配器ArrayAdapter,將功能表項目放在適配器中,
添加刪除功能表項目只需要調用適配器的add,remove方法即可。

布局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"
>
<EditText android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
/>
<Button android:id="@+id/remove"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刪除"
/>
<Spinner android:id="@+id/sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

一個EditText,用於定義需要添加或刪除的功能表項目,一個添加,一個刪除按鈕,還有一個就是Spinner
在strings.xml中定義一個初始的數組,就是剛開始時Spinner顯示的項目,當然,也可以直接在JAVA代碼中定義

<string-array name="action">
<item>吃飯</item>
<item>睡覺</item>
<item>上網</item>
</string-array>

JAVA程式碼:

package com.pocketdigi.spanner;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

publicclass main extends Activity {
/** Called when the activity is first created. */
EditText et;
Button add,remove;
Spinner sp;
ArrayList<String> list=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.et);
add=(Button)findViewById(R.id.add);
remove=(Button)findViewById(R.id.remove);
sp=(Spinner)findViewById(R.id.sp);
//擷取相應對象
String[] ls=getResources().getStringArray(R.array.action);
//擷取XML中定義的數組
for(int i=0;i<ls.length;i++){
list.add(ls[i]);
}
//把數組匯入到ArrayList中
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//設定下拉式功能表的風格
sp.setAdapter(adapter);
//綁定適配器
sp.setPrompt("標題列");
//設定對話方塊標題列
add.setOnClickListener(new OnClickListener(){//添加按鈕監聽器

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
adapter.add(et.getText().toString());
//添加輸入的項 ,add後自動調用notifyDataSetChanged()
//如果需要指定位置,使用insert(String s, int index)方法
setTitle(String.valueOf(list.size()));
//在標題輸出添加後list的大小
}

});
remove.setOnClickListener(new OnClickListener(){//刪除按鈕監聽器

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
adapter.remove(sp.getSelectedItem().toString());
//刪除當前選中項,remove後自動調用notifyDataSetChanged()
setTitle(String.valueOf(list.size()));
}

});

}
}

轉自:http://www.pocketdigi.com/20100810/20.html

相關文章

聯繫我們

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