公海赌船备用网址&gt 创建下拉绳的数源 

本文转自:http://blog.csdn.net/wanli_smile/article/details/6410388

android:Spinner(下拉框)控件的下

 

创造页面文件(main.xml)  

于是法 1 :以资源方式,静态展示 Spinner 选项

 1     <Spinner

1.     在资源文件 (strings.xml) 中,增加:

 2         android:id=”@+id/spinner1″

         < string name = “spin_prompt” > 请选择城市 </ string
>

 3         android:layout_width=”match_parent”

         < string-array name = “cities” >

 4         android:layout_height=”wrap_content” />

                   < item > 北京 </ item >

 5 

                   < item > 上海 </ item >

 6     <TextView

                   < item > 南京 </ item >

 7         android:id=”@+id/tvResult”

                   < item > 乌鲁木齐 </ item >

 8         android:layout_width=”match_parent”

                   < item > 哈尔滨 </ item >

 9         android:layout_height=”wrap_content”

                   < item > 符拉迪沃斯托克 </ item >

10         android:text=”选择的水果” />

         </ string-array >

 

 

 创建下拉绳的数码源 

2.     在布局文件 (main.xml) 中,增加:

 

         < Spinner

1        List<String> list = new ArrayList<String>();

                   android:id = “@+id/Spinner01”

2        list.add(“苹果”); 

                   android:layout_width = “wrap_content”

5        list.add(“香蕉”); 

                   android:layout_height = “wrap_content”

7        list.add(“橘子”); 

                   android:entries = “@array/cities”

9        list.add(“香蕉”);

                   android:prompt = “@string/spin_prompt”

 

         />

 创建适配器(下拉框的数据源是出自适配器) 

         注意: android:prompt 必须使引用 strings.xml 中资源 ID
,而不能够当这里一直用 raw text 。

 

 

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

         运行结果:

 为适配器添加样式      

 公海赌船备用网址 1

 

 

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

点击Spinner右边的带倒三角的按钮,即出现各城市选择如下:

 

 

         系统提供的样式如下

公海赌船备用网址 2
为此法 2 :以代码方式,动态展示 Spinner 选项

 

1.    
在资源文件 strings.xml 中,无需定义任何资源。

l  simple_spinner_dropdown_item(列表-间距较高比较为难)

2.    
在布局文件 (main.xml) 中, Spinner 的讲述为:

 

         <
Spinner

clip_image004[10]

                  
android:id = “@+id/Spinner01”

 

                  
android:layout_width = “wrap_content”

l  simple_spinner_item(列表-间距紧凑不难堪)

                 
android:layout_height = “wrap_content”

 

        
/>

clip_image006[10]

3.    
在 Activity 对应之代码中开相关修改,使的如下:

 

public
class ControlSpinner extends Activity

l  simple_list_item_checked(复选框-选中的出绿沟)

{

 

                  
private static final String tag = “ControlSpinner” ;

clip_image008[10]

                  
private static final String[] cities =

 

                           
{ ” 上海 ” , ” 北京 ” , ” 南京 ” , ” 哈尔滨 ” , ” 乌鲁木齐 ” , ”
符拉迪沃斯托克 ” , ” 圣弗朗西斯科 ” };
 
               private Spinner spinner ;

l  simple_list_item_single_choice (单选按钮)

              
@Override

 

              
public void onCreate(Bundle savedInstanceState)

clip_image010[10]

              
{

 

                   
super .onCreate(savedInstanceState);

 向控件Spinner添加适配器 

                   
setContentView(R.layout. main );

 

 

1       Spinner sp=(Spinner) findViewById(R.id.spinner1);

                   
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this , android.R.layout. simple_spinner_item , cities );
 
//      ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
 
//      adapter.add(” 上海 “);

2       sp.setAdapter(adapter); 

//     
adapter.add(” 北京 “);

 

//     
adapter.add(” 南京 “);

     释义 

//     
adapter.add(” 哈尔滨 “);

 

//     
adapter.add(” 乌鲁木齐 “);

     1. setAdapter(SpinnerAdapter adapter) ,从类的接续关系及看,ArrayAdapter是SpinnerAdapter接口的间接实现类似

//     
adapter.add(” 符拉迪沃斯托克 “);

 

//     
adapter.add(” 圣弗朗西斯科 “);

clip_image012[10] 

                   
adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item );
 
                    spinner = (Spinner) this .findViewById(R.id.
Spinner01 );

 

          
spinner .setAdapter(adapter);

 实现选择项事件(使用匿名类实现接口)  

                   
spinner .setPrompt( ” 请选择都 : ” );

 

              
}

 1         sp.setOnItemSelectedListener(new OnItemSelectedListener() {

                  
… …

 2             // parent: 为控件Spinner   view:显示文字的TextView   position:下拉选项之职位从0开始  

         }

 3             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        
运行效果与用法 1 同样。

 4                 TextView tvResult = (TextView) findViewById(R.id.tvResult);

 

 5                 //获取Spinner控件的适配器

        
注意:

 6                 ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter();

1.    
被诠释的代码可以代替

 7                 tvResult.setText(adapter.getItem(position));

private
static final String[] cities = { ” 上海 ” , ” 北京 ” , ” 南京 ” , ”
哈尔滨 ” , ” 乌鲁木齐 ” , ” 符拉迪沃斯托克 ” , ” 圣弗朗西斯科 ” };
 

 8             }

ArrayAdapter<String>
adapter = new ArrayAdapter<String>( this , android.R.layout.
simple_spinner_item , cities );
 
有数长达告句之组成。

 9              //没有当选时之处理

2.    
ArrayAdapter 构造函数中的老二个参数是 android.R.layout.
simple_spinner_item , adapter.setDropDownViewResource 中的参数则是
android.R.layout. simple_spinner_dropdown_item 。它们是不一样的。
 
 

10             public void onNothingSelected(AdapterView<?> parent) {

 

11             }

故此法 3
:混合方式

12         });

1.    
在资源文件 (strings.xml) 中,相关的资源描述为:

 

 

 后台整体代码如下  

2.    
在布局文件 (main.xml) 中, Spinner 的叙说为:

 

         <
Spinner

 1     public void onCreate(Bundle savedInstanceState) {

                  
android:id = “@+id/Spinner01”

 2         super.onCreate(savedInstanceState);

                  
android:layout_width = “wrap_content”

 3         setContentView(R.layout.main);

                  
android:layout_height = “wrap_content”

 4         List<String> list = new ArrayList<String>();

        
/>

 5         list.add(“苹果”);

3.    
在 Activity 对应之代码中做连锁修改,使的如下:

 6         list.add(“香蕉”);

public
class ControlSpinner extends Activity

 7         list.add(“橘子”);

{

 8         list.add(“香蕉”);

              
private Spinner spinner ;

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

              
@Override

10         adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

              
public void onCreate(Bundle savedInstanceState)

11         Spinner sp = (Spinner) findViewById(R.id.spinner1);

              
{

12         sp.setAdapter(adapter);

                   
super .onCreate(savedInstanceState);

13         sp.setOnItemSelectedListener(new OnItemSelectedListener() {

                   
setContentView(R.layout. main );

14             // parent: 为控件Spinner   view:显示文字的TextView   position:下拉选项之职从0开始  

 

15             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                   
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource ( this ,

16                 TextView tvResult = (TextView) findViewById(R.id.tvResult);

                            
R.array. cities ,

17                 //获取Spinner控件的适配器

                            
android.R.layout. simple_spinner_item );

18                 ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter();

                   
adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item );
 
                    spinner = (Spinner) this .findViewById(R.id.
Spinner01 );

19                 tvResult.setText(adapter.getItem(position));

                   
spinner .setAdapter(adapter);

20             }

                   
spinner .setPrompt( ” 请选择城市: ” );

21              //没有入选时的拍卖

              
}

22             public void onNothingSelected(AdapterView<?> parent) {

                  
… …

23             }

         }

24         });

运行效果和办法
1 呢同。

25     }

 

 

注意:

1.    
ArrayAdapter<CharSequence> adapter 中的沙盘参数必须为 CharSequence

2.    
createFromResource 的第二单参数 R.array.cities 就是在 strings.xml
中定义之资源

 

 

从而法 4
:改变 Spinner 对象自我的分寸,及其文字的分寸与颜色

1.    
在布局文件 (main.xml) 中,修改 Spinner 对象的描述如下:

         <
Spinner

                  
android:id = “@+id/Spinner01”

                  
android:layout_width = “160px”

                  
android:layout_height = “40px”

        
/>

即时叫
Spinner01 的大幅度为 160 独像素,高度也 40 个像素。

2.    
创建一个搭架子文件 spinnerLayout.xml ,使之如下:

<?
xml version = “1.0” encoding = “utf-8” ?>

         <
TextView xmlns:android = “http://schemas.android.com/apk/res/android”

   
android:layout_width = “fill_parent”

              
android:layout_height = “wrap_content”

              
android:textSize = “12sp”         

              
android:textColor = “#FF8B1500”

              
android:gravity = “center”

        
/>

3.    
在 Activity 对应之代码中举行相关修改,使的如下:

public
class ControlSpinner extends Activity

{

              
private Spinner spinner ;

   
@Override

              
public void onCreate(Bundle savedInstanceState)

              
{

                   
super .onCreate(savedInstanceState);

                   
setContentView(R.layout. main );

 

                   
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource ( this ,

                            
R.array. cities ,

                            
R.layout. spinnerlayout
);                                                                    
// 改为 spinnerlayout
 
                    adapter.setDropDownViewResource(R.layout.
spinnerlayout );           // 改为 spinnerlayout
 
                    spinner = (Spinner) this .findViewById(R.id.
Spinner01 );

                   
spinner .setAdapter(adapter);

                   
spinner .setPrompt( ” 请选择都 : ” );

              
}

                  
… …

         }

运作结果如下:

 

公海赌船备用网址 3

 

公海赌船备用网址 4

 

 

 

之所以法5:同时显示图片及文件

只要有澳大利亚、瑞士、中华人民共和国和美利坚合众国这4只国,我们怀念管其的国旗和它们的国名同时显示在Spinner中。

它对应之国旗图片资源分别如下:

公海赌船备用网址 5公海赌船备用网址 6公海赌船备用网址 7公海赌船备用网址 8
对应之图形文件称分别吗: au.png, ch.png, cn.png 和 us.png 。

 

1.    
首先将国旗图片拖入 res/drawable-mdpi 文件夹。系统会自动吗我们于 R.java
中生成对应的 id 。情形如下:

    public static final
class drawable {

        public static
final int au=0x7f020000;

        public static
final int ch=0x7f020001;

        public static
final int cn=0x7f020002;

        public static
final int icon=0x7f020004;       // 系统自带的 icon 图标

        public static
final int us=0x7f020005;

    }

 

2.     在 strings.xml
中于闹各个国家的国名。

         < string
name = “au” > 澳大利亚 </ string >

         < string
name = “ch” > 瑞士 </ string >

         < string
name = “cn” > 中华人民共和国 </ string >

         < string
name = “us” > 美利坚合众国 </ string >

 

保留后,系统也会自动为咱以
R.java 中变化对应的 id 。情形如下:

    public static final
class string {

        public static
final int app_name=0x7f040001;              // 系统自带的 string
 
        public static final int au=0x7f040002;

        public static
final int ch=0x7f040003;

        public static
final int cn=0x7f040004;

        public static
final int us=0x7f040006;

    }

 

3.     假定在布局文件
(main.xml) 中, Spinner01 的属性如下:

         < Spinner

                  
android:id = “@+id/Spinner01”

                  
android:layout_width = “300px”

                  
android:layout_height = “48px”

/>

 

4.     在 Activity
对应之代码中做连锁修改,使的如下:

public class
ControlSpinner extends Activity

{

               private
Spinner spinner ;

                //
国旗图片 ID 数组

               private
int [] drawableIDs = {

                       
R.drawable. au ,

                       
R.drawable. ch ,

                       
R.drawable. cn ,

                       
R.drawable. us

               };

 

                   //
国名文字 ID 数组

               private
int [] nameIDs = {

                       
R.string. au ,

                       
R.string. ch ,

                       
R.string. cn ,

                       
R.string. us

               };

   

                   //
自定义一个 Adapter ,要再次写 getCount 、 getItem 、 getItemId 和 getView
方法。其中的 getView 方法极其关键。
 
                   // 当然也得预先独立定义一个 Adapter 类

               private
BaseAdapter customizedAdapter = new BaseAdapter()

               {

                           
public int getCount()

                            {

                                    
// TODO Auto-generated method stub

                                    
return drawableIDs . length ;

                            }

 

                           
public Object getItem( int position)

                            {

                                    
// TODO Auto-generated method stub

                                    
return drawableIDs [position];

                           }

 

                           
public long getItemId( int position)

                           {

                                    
// TODO Auto-generated method stub

                                    
return position;

                            }

 

                           
public View getView( int position, View convertView, ViewGroup parent)
 
                           {

                                    
// TODO Auto-generated method stub

                                    
// 先设定一个 LinearLayout 对象 ll

                                    
LinearLayout ll = new LinearLayout(ControlSpinner. this );
 
                                     // 使 ll 的 Orientation 为
HORIZONTAL

                                    
ll.setOrientation(LinearLayout. HORIZONTAL );
 
                                     // 在笔直方向居中

                                    
ll.setGravity(Gravity. CENTER_VERTICAL );

                           

                                    
// 创建一个 ImageView 对象

                                    
ImageView iv = new ImageView(ControlSpinner. this );
 
                                     // 指定对应 position 的 Image

                                    
iv.setImageResource( drawableIDs [position]);
 
                                     // 设定 ImageView 对象 iv 的幅度为
100 像素,高度为 40 像素
 
                                     iv.setLayoutParams( new
ViewGroup.LayoutParams(100, 40));
 
                                     // 将 iv 加入到 ll

                                    
ll.addView(iv);                    

                           

                                    
// 创建一个 TextView 对象

                                    
TextView tv = new TextView(ControlSpinner. this );
 
                                     // 指定对应 position 的 Text

                                    
tv.setText( nameIDs [position]);

                                    
// 设定文字大小

                                    
tv.setTextSize(14);

                                    
// 设定文字颜色

                                    
tv.setTextColor(Color. BLUE );

                                    
// 将 tv 加入到 ll

                                    
ll.addView(tv);

                           

                                    
return ll;

                            }

                  };

                  

               @Override

               public
void onCreate(Bundle savedInstanceState)

               {

                   
super .onCreate(savedInstanceState);

                   
setContentView(R.layout. main );

 

                   
spinner = (Spinner) this .findViewById(R.id. Spinner01 );

                   
spinner .setAdapter( customizedAdapter );

                   
spinner .setPrompt( ” 请选择国家: ” );

               }

                   … …

         }

         运行结果如下:

 运行结果如下:

公海赌船备用网址 9

公海赌船备用网址 10

 
如果单独建一个 CustomizedAdapter ,相关代码改写如下:

        
class CustomizedAdapter extends BaseAdapter

{

                  
private Context ctx ;

                  
private int drawableIDs [];

                  
private int stringIDs [];

                  
public CustomizedAdapter(Context ctx, int DrawableIDs[], int
StringIDs[])
 
                   {

                           
this . ctx = ctx;

                           
this . drawableIDs = DrawableIDs;

                           
this . stringIDs = StringIDs;

                  
}

        

                  
public int getCount()

                  
{

                           
// TODO Auto-generated method stub

                           
return drawableIDs . length ;

                  
}

 

                  
public Object getItem( int position)

                  
{

                           
// TODO Auto-generated method stub

                           
return drawableIDs [position];

                  
}

 

                  
public long getItemId( int position)

                  
{

                           
// TODO Auto-generated method stub

                           
return position;

                  
}

 

                  
public View getView( int position, View convertView, ViewGroup parent)
 
                   {

                           
// TODO Auto-generated method stub

                           
LinearLayout ll = new LinearLayout( ctx );

                           
ll.setOrientation(LinearLayout. HORIZONTAL );

                           
ll.setGravity(Gravity. CENTER_VERTICAL );

                  

                           
ImageView iv = new ImageView( ctx );

                           
iv.setImageResource( drawableIDs [position]);

                           
iv.setLayoutParams( new ViewGroup.LayoutParams(100, 40));
 
                            ll.addView(iv);                    

                  

                           
TextView tv = new TextView( ctx );

                           
tv.setText( stringIDs [position]);

                           
tv.setTextSize(14);

                           
tv.setTextColor(Color. BLUE );

                           
ll.addView(tv);

                  

                           
return ll;

                  
}

}

 

 

        
Activity 对应之代码改写如下:

public
class ControlSpinner extends Activity

{

   
private Spinner spinner ;

   

              
private int [] drawableIDs = {

                       
R.drawable. au ,

              
R.drawable. ch ,

                       
R.drawable. cn ,

                       
R.drawable. us

              
};

   
private int [] nameIDs = {

                       
R.string. au ,

                       
R.string. ch ,

              
R.string. cn ,

                       
R.string. us

              
};

                  

   
@Override

   
public void onCreate(Bundle savedInstanceState)

         {

       
super .onCreate(savedInstanceState);

                 
setContentView(R.layout. main );

 

              
CustomizedAdapter customizedAdapter = new CustomizedAdapter( this ,
drawableIDs , nameIDs );
 
               spinner = (Spinner) this .findViewById(R.id. Spinner01 );

           
spinner .setAdapter(customizedAdapter);

           
spinner .setPrompt( ” 请选择国家 : ” );

       
}

         … …

      
}

        
所获的效益,和前边的气象是同等的。

 

        

故法 6
:获取所选择型之文书内容

1.    
正常状况下 ( 用法 1~ 用法 4 所展示之情形 )
,我们好由此这样的道,来获取 Spinner 对象被所选型之文件内容:

TextView
tx_spinner = (TextView) spinner .getSelectedView();

String
str = (String) tx_spinner.getText();

Toast.makeText
( this , str, Toast. LENGTH_LONG ).show();

 

2.    
但是只要我们以用法 5 中自定义之 Adapter
,就用做一些微的转,才会健康得到 Spinner
所选项目的仿内容。因为, CustomizedAdapter 中之 getView
方法,最后回来的凡一个 LinearLayout 对象 (LinearLayout 继承了 View)
,而以 LinearLayout 对象中,又带有了一个 ImageView 和一个 TextView
。所以,
 
TextView tx_spinner = (TextView)spinner .getSelectedView();

随即句话就是不再使用了。因为
spinner.getSelectView() 会返回一个蕴含了一个 ImageView 和一个 TextView
的 LinearLayout 对象,而不再是一个单的 TextView 了。
 
 

        
为这,我们以布局文件 main.xml 中,增加一个 Button
,因为咱们纪念在点击一个按钮的景象下,获取 Spinner 中所选中的亲笔外
 
容。如下:

         <
Button

                  
android:id = “@+id/Button01”

                  
android:layout_width = “120px”

                  
android:layout_height = “60px”

                  
android:text = “OK”

        
/>

        

         用法
5 中之 CustomizedAdapter 中之 getView 方法要加一行代码 ( 粗体字 )
,修改后使下

        
public View getView( int position, View convertView, ViewGroup parent)

         {

                  
// TODO Auto-generated method stub

                  
LinearLayout ll = new LinearLayout( ctx );

                  
ll.setOrientation(LinearLayout. HORIZONTAL );

                  
ll.setGravity(Gravity. CENTER_VERTICAL );

                  

                  
ImageView iv = new ImageView( ctx );

                  
iv.setImageResource( drawableIDs [position]);

                  
iv.setLayoutParams( new ViewGroup.LayoutParams(100, 40));

                  
ll.addView(iv);                    

                  

                  
TextView tv = new TextView( ctx );

                  
tv.setText( stringIDs [position]);

                  
tv.setTextSize(14);

                  
tv.setTextColor(Color. BLUE );

                  
tv.setTag( “tagTextView” );                 // 为 TextView 对象多一个
Tag , 以便在继承之处理中 , 可以由此
 
                  
ll.addView(tv);                                        //
findViewWithTag 方法来取之 TextView 对象
 
                  

                  
return ll;

         }

 

 

         修改
Activity 所对应之代码,使的如下:

        
public class ControlSpinner extends Activity

implements

OnClickListener

{

              
private Spinner spinner ;

              
… …

                  

   
@Override

              
public void onCreate(Bundle savedInstanceState)

              
{

                   
super .onCreate(savedInstanceState);

          
setContentView(R.layout. main );

 

                   
CustomizedAdapter customizedAdapter = new CustomizedAdapter( this ,
drawableIDs , nameIDs );
 
                    spinner = (Spinner) this .findViewById(R.id.
Spinner01 );

                   
spinner .setAdapter(customizedAdapter);

                   
spinner .setPrompt( ” 请选择国家 : ” );

 

                   
// 获取按钮对象并也的多监听器 :

                   
Button button = (Button) this .findViewById(R.id. Button01 );
 
                    button.setOnClickListener( this );

              
}

 

                 
public void onClick(View v)

                 
{

                           
// TODO Auto-generated method stub

                   
if (v.getId() == R.id. Button01 )

                   
{

                                    
// 方法 1~ 方法 4 的景,可以使用如下被诠释的的代码:

//
TextView tx_spinner = (TextView)spinner.getSelectedView();

//
String str = (String) tx_spinner.getText();

//
Toast.makeText(this, str, Toast.LENGTH_LONG).show();

 

// 方法
5 所列的景况,需用如下方式赢得 Spinner 所选中项目的亲笔内容

                             
LinearLayout ll = (LinearLayout) spinner .getSelectedView();
 
                              TextView tv =
(TextView)ll.findViewWithTag( “tagTextView” );         // 得到相应的
TextView 对象
 
                              String str = (String)tv.getText();

                             
Toast.makeText ( this , str, Toast. LENGTH_LONG ).show();
 
                    }

                 
}

                  
… …

         }

        
其他代码和用法 5 中之代码一致。运行结果:

 公海赌船备用网址 11

可见得对地获得Spinner所选项目中的文内容。

故而法 7 :在
onItemSelected 方法被,获取所选择型之文书内容。

1.     首先让 Activity
实现 OnItemSelectedListener 接口

2.     给 spinner
对象多 OnItemSelectedListener

3.     实现
OnItemSelectedListener 接口中定义之蝇头只接口方法 onItemSelected 和
onNothingSelected

代码如下:

public class
ControlSpinner extends Activity

implements

OnClickListener,
OnItemSelectedListener

{

               private
Spinner spinner ;

                  … …

               @Override

               public
void onCreate(Bundle savedInstanceState)

               {

                   
super .onCreate(savedInstanceState);

                   
setContentView(R.layout. main );

 

                   
CustomizedAdapter customizedAdapter = new CustomizedAdapter( this ,
drawableIDs , nameIDs );
 
                    spinner = (Spinner) this .findViewById(R.id.
Spinner01 );

                   
spinner .setAdapter(customizedAdapter);

                   
spinner .setPrompt( ” 请选择国家 : ” );

 

                    //
获取按钮对象并为之设定 listener :

                   
Button button = (Button) this .findViewById(R.id. Button01 );
 
                    button.setOnClickListener( this );

       

                    //
为 spinner 设定 listener :

                   
spinner .setOnItemSelectedListener( this );

               } 

   

               public
void onItemSelected(AdapterView<?> parent, View view, int
position, long id)

               {

                            //
以下代码,适用于用法 1~4 中的情

                            //
if(parent.getId() == R.id.Spinner01)

                            //
{

                           
//      Toast.makeText(this, ((TextView)view).getText(),
Toast.LENGTH_LONG).show();//tv.getText();
 
                            // }

                  

                            if
(parent.getId() == R.id. Spinner01 )

                            {

                                     
LinearLayout ll = (LinearLayout)view;

                                     
TextView tv = (TextView)ll.findViewWithTag( “tagTextView” );

                            
String str = (String)tv.getText();

                            
Toast.makeText ( this , str, Toast. LENGTH_LONG ).show();

                           
}                                                                                                                                                                             

               }

                                                        

                  public
void onNothingSelected(AdapterView<?> parent)

                  {

                  }

                   … …

}

         其他代码和用法
6 同。

 

里面, onItemSelected
方法解释如下:

public abstract void
onItemSelected (AdapterView <?> parent, View view, int position,
long id)
 
Callback method to be invoked when an item in this view has been
selected. Impelmenters can call getItemAtPosition(position) if they need
to access the data associated with the selected item.
 
 

Parameters

        
parent               The AdapterView where the selection happened

view                 
The view within the AdapterView that was clicked

position            The
position of the view in the adapter

id                        The
row id of the item that is selected

 

相关文章