动态操作Spinner控件实例

这篇博客介绍了如何在Android应用中动态地添加和删除Spinner控件的项目,通过编辑框输入内容,点击按钮实现Spinner内容的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本实例是通过在编辑框内输入新的Spinner项目,然后点击删除和添加按钮动态改变Spinner的实例。

下面是具体代码:

xml文件中代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textSize="20sp" />
    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textSize="20sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:text="添 加"
            android:textSize="20sp" />
        <Button
            android:id="@+id/button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删 除"
            android:textSize="20sp" />
    </LinearLayout>
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />
</LinearLayout>

主程序Activity类中的代码:

package com.example.androidnetworkprogram;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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;
import android.widget.TextView;
public class DaynamicSpinner extends Activity {
 private TextView mTextView = null;
 private EditText mEditText = null;
 private Button addButton = null;
 private Button removeButton = null;
 private Spinner mySpinner = null;
 private String[] spinnerStrArr = null;
 private ArrayAdapter<String> myAdapter = null;
 private List<String> myList = null;
 private AlertDialog.Builder myAlertDialog = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dynamicspinner);
  mySpinner = (Spinner)this.findViewById(R.id.spinner1);
  mTextView = (TextView)this.findViewById(R.id.textview1);
  mEditText = (EditText)this.findViewById(R.id.edittext1);
  addButton = (Button)this.findViewById(R.id.button11);
  removeButton = (Button)this.findViewById(R.id.button12);
  spinnerStrArr = getResources().getStringArray(R.array.application);
  myList = new ArrayList<String>();
  for(int i=0;i<spinnerStrArr.length;i++){
   myList.add(spinnerStrArr[i]);
  }
  myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,myList);
  myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  mySpinner.setAdapter(myAdapter);
  myAlertDialog = new AlertDialog.Builder(DaynamicSpinner.this);
  myAlertDialog.setTitle("警告!!!");
  myAlertDialog.setMessage("请填入信息啊");
  myAlertDialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface arg0, int arg1) {
   }
  });
  myAlertDialog.setNegativeButton("取消", null);
  addListenerToButton();
 }
 public void addListenerToButton(){
  addButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
    String tempStr = mEditText.getText().toString().trim();
    if(!tempStr.equals("")){
     myAdapter.add(tempStr);
     int position = myAdapter.getPosition(tempStr);
     mySpinner.setSelection(position);
    }
    else{
     myAlertDialog.show();
    }
   }
  });
  removeButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View arg0) {
    String tempStr = mEditText.getText().toString().trim();
    if(myAdapter.getPosition(tempStr)>=0){
     myAdapter.remove(tempStr);
     int position = myAdapter.getPosition(tempStr);
     mySpinner.setSelection(position);
    }
    else{
     myAlertDialog.setMessage("你输入的信息不存在");
     myAlertDialog.show();
    }
   }
  });
 }
}

资源数组xml中的代码(放在res/value/string.xml文件中)

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

    <string name="app_name">androidnetworkprogram</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="application">
        <item>genymotion</item>
        <item>eclipse</item>
        <item>matlab</item>
        <item>visual studio</item>
        <item>VC 6.0++</item>
        <item>Tencent QQ</item>
    </string-array>

</resources>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值