Android下拉列表框

下拉列表框

        下拉列表的功能在开发之中也同样常见,但是需要注意的是,在Android之中下拉列表的实现依靠Spinner组件完成。

        但是对于Spinner而言肯定要由多个下拉选项所组成,那么这些个下拉选项的内容可以直接利用配置文件完成。

范例:建立一个保存程序信息的列表内容—— values/city_data.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

   <string-arrayname="city_labes">

   <item>北京</item>

   <item>上海</item>

   <item>广州</item>

   <item>辽宁</item>

   <item>南京</item>

   </string-array>

</resources>

        在city_data.xml文件中的内容就是以后向Spinner组件之中填充的数据。

<?xmlversion="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:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="请选择你喜欢的城市:"/>

   <Spinner

      android:id="@+id/city"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:entries="@array/city_labes"/>  à配置要的内容

   <TextView

      android:id="@+id/msg"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"/>

</LinearLayout>

        此时是通过配置文件完成了所有的内容输入。但是如果要想对下拉列表框进行事件的处理操作,则可以使用选项选中的事件:public voidsetOnItemSelectedListener (AdapterView.OnItemSelectedListenerlistener)

package cn.mldn.demo;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Spinner;

import android.widget.TextView;

public class MyActivity extendsActivity {

   publicstatic finalString TAG= "MyActivity";

   privateSpinner city = null;

   privateTextView msg = null;

   @Override

   publicvoid onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      super.setContentView(R.layout.main);

      this.city= (Spinner) super.findViewById(R.id.city);

      this.msg= (TextView) super.findViewById(R.id.msg);

      this.city.setOnItemSelectedListener(newOnItemSelectedListenerImpl());

   }

   privateclass OnItemSelectedListenerImplimplements OnItemSelectedListener {

      publicvoid onItemSelected(AdapterView<?>parent, View view,

             intposition, longid) {

          String value =parent.getItemAtPosition(position).toString();

          MyActivity.this.msg.setText("选择的内容是:"+ value);

      }

      publicvoid onNothingSelected(AdapterView<?>parent) {

          Log.i(TAG,"** 没有选项被选中。");

      }

   }

}

        这个时候的下拉框是通过配置文件完成的,同样下面换另外一种方式实验一下,如果说现在所有的内容都是在程序中固定的好的,那么又如何呢?

   <Spinner

      android:id="@+id/city"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"/>

        如果通过程序配置,那么肯定不能在配置文件之中编写entries属性,如果要想利用程序完成,则必须使用如下一个方法,此方法为设置数据封装适配器:

public voidsetAdapter (SpinnerAdapter adapter)

        这个方法是可以将数组(或集合)内容进行转换,这个方法需要一个SpinnerAdapter接口的对象,那么现在既然这个是接口,那么肯定继续找子类,可以使用一个最简单的子类,因为现在所有的内容都是利用了字符串保存的,那么就利用ArrayAdapter<T>子类,这个子类的构造方法:

publicArrayAdapter (Context context, int textViewResourceId, T[]objects)

范例:定义Activity程序,操作内容

package cn.mldn.demo;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.SpinnerAdapter;

import android.widget.TextView;

public class MyActivity extendsActivity {

   publicstatic finalString TAG= "MyActivity";

   privateSpinner city = null;

   privateTextView msg = null;

   privateString data[] =new String[] { "北京","上海","天津","南京","南宁"};

   privateSpinnerAdapter adapter= null;

   @Override

   publicvoid onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      super.setContentView(R.layout.main);

      this.city= (Spinner) super.findViewById(R.id.city);

      this.msg= (TextView) super.findViewById(R.id.msg);

      this.adapter= newArrayAdapter<String>(this,

             android.R.layout.select_dialog_item,this.data);

      this.city.setAdapter(this.adapter) ;

      this.city.setOnItemSelectedListener(newOnItemSelectedListenerImpl());

   }

   privateclass OnItemSelectedListenerImplimplements OnItemSelectedListener {

      publicvoid onItemSelected(AdapterView<?>parent, View view,

             intposition, longid) {

          String value =parent.getItemAtPosition(position).toString();

          MyActivity.this.msg.setText("选择的内容是:"+ value);

      }

      publicvoid onNothingSelected(AdapterView<?>parent) {

          Log.i(TAG,"** 没有选项被选中。");

      }

   }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值