android之组件2

   

第一:

Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:

在样式文件中:

<?xml version="1.0" encoding="utf-8"?>  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.       
  6.     <TextView   
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="请选择一项运动项目"  
  10.         />  
  11.     <Spinner   
  12.     android:id ="@+id/sportsSp"  
  13.     android:layout_width="match_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:prompt="@string/spinner_prompt"  
  16.     android:entries="@array/sports"  
  17.     />  
  18.       
  19. </LinearLayout>  


在<Spinner>标签中,通过android:prompt来设置弹出选择框的标题,通过android:entries来设置默认的列表选项(定义在一个arrays.xml文件中)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="sports" >  
  4.         <item>足球</item>  
  5.         <item>篮球</item>  
  6.         <item>乒乓球</item>  
  7.         <item>网球</item>       
  8.     </string-array>  
  9. </resources>  

监听列表点击事件:

  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemSelectedListener;  
  9. import android.widget.Spinner;  
  10. import android.widget.TextView;  
  11.   
  12. public class SpinnerDemo extends Activity implements OnItemSelectedListener{  
  13.     Spinner sportSp = null;  
  14.       
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.spinner_layout);  
  18.         findViews();  
  19.     }  
  20.   
  21.     private void findViews() {  
  22.         sportSp = (Spinner)this.findViewById(R.id.sportsSp);  
  23.         sportSp.setOnItemSelectedListener(this);  
  24.         sportSp.performClick();  
  25.     }  
  26.     //每选择一次均以日志输出形式打印  
  27.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  28.             long arg3) {  
  29.         TextView tv = (TextView) arg1;  
  30.         Log.i("TAG", tv.getText().toString());  
  31.     }  
  32.   
  33.     public void onNothingSelected(AdapterView<?> arg0) {  
  34.     }  
  35. }  

在模拟器中的效果与日志输出结果:

 

 

第二:

ListView列表视图,为列表添加列表项有两种方法,下面用一个列子分别介绍:

在样式文件中:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="名单:"  
  11.         />  
  12.       
  13.     <ListView  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:entries="@array/sports"  
  17.         android:divider="#00FF00"  
  18.         />  
  19.       
  20.     <ListView  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:id="@+id/nameList"  
  24.         android:divider="#00FF00"  
  25.         />  
  26.   
  27. </LinearLayout>  


在上面的布局中定义了两个ListView,第一个ListView通过android:entries指定了列表的项数组:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="sports" >  
  4.         <item>足球</item>  
  5.         <item>篮球</item>  
  6.         <item>乒乓球</item>  
  7.         <item>网球</item>       
  8.     </string-array>  
  9. </resources>  


第二个ListView通过ArrayAdapter适配器使用数组来确定列表项,并监听点击事件,每点击用日志输出打印:

  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemClickListener;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.ListView;  
  11.   
  12. public class ListViewDemo extends Activity  
  13.     implements OnItemClickListener{  
  14.   
  15.     ListView nameList = null;  
  16.     String[] names = {"张三","李四","王五","宋六","猪八"};  
  17.       
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         this.setContentView(R.layout.list_layout);  
  21.         findViews();  
  22.     }  
  23.   
  24.     private void findViews() {  
  25.         nameList = (ListView) this.findViewById(R.id.nameList);  
  26.         //定义一个适配器,同时将定义列表项的数组添加进去  
  27.         ArrayAdapter adapter = new ArrayAdapter(this,  
  28.                 android.R.layout.simple_list_item_1,names);  
  29.         nameList.setAdapter(adapter);  
  30.         //为列表添加监听事件  
  31.         nameList.setOnItemClickListener(this);  
  32.     }  
  33.   
  34.     //覆盖监听器接口OnItemClickListener的抽象方法  通过日志打印所点击的列表项信息  
  35.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  36.         Log.i("TAG", names[arg2]  
  37.                 + " position=" + String.valueOf(arg2)  
  38.                 +"  row_id=" + String.valueOf(arg3) );  

第三:

Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:

在样式文件中:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="请选择一项运动项目"  
  11.         />  
  12.     <Spinner   
  13.     android:id ="@+id/sportsSp"  
  14.     android:layout_width="match_parent"  
  15.     android:layout_height="wrap_content"  
  16.     android:prompt="@string/spinner_prompt"  
  17.     android:entries="@array/sports"  
  18.     />  
  19.       
  20. </LinearLayout>  


在<Spinner>标签中,通过android:prompt来设置弹出选择框的标题,通过android:entries来设置默认的列表选项(定义在一个arrays.xml文件中)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="sports" >  
  4.         <item>足球</item>  
  5.         <item>篮球</item>  
  6.         <item>乒乓球</item>  
  7.         <item>网球</item>       
  8.     </string-array>  
  9. </resources>  


监听列表点击事件:

  1. package cn.class3g.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.widget.AdapterView;  
  8. import android.widget.AdapterView.OnItemSelectedListener;  
  9. import android.widget.Spinner;  
  10. import android.widget.TextView;  
  11.   
  12. public class SpinnerDemo extends Activity implements OnItemSelectedListener{  
  13.     Spinner sportSp = null;  
  14.       
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.spinner_layout);  
  18.         findViews();  
  19.     }  
  20.   
  21.     private void findViews() {  
  22.         sportSp = (Spinner)this.findViewById(R.id.sportsSp);  
  23.         sportSp.setOnItemSelectedListener(this);  
  24.         sportSp.performClick();  
  25.     }  
  26.     //每选择一次均以日志输出形式打印  
  27.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  28.             long arg3) {  
  29.         TextView tv = (TextView) arg1;  
  30.         Log.i("TAG", tv.getText().toString());  
  31.     }  
  32.   
  33.     public void onNothingSelected(AdapterView<?> arg0) {  
  34.     }  
  35. }  


在模拟器中的效果与日志输出结果:



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值