android之组件2
第一:
Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:
在样式文件中:
<?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请选择一项运动项目"
- />
- <Spinner
- android:id="@+id/sportsSp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:prompt="@string/spinner_prompt"
- android:entries="@array/sports"
- />
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <string-arrayname="sports">
- <item>足球</item>
- <item>篮球</item>
- <item>乒乓球</item>
- <item>网球</item>
- </string-array>
- </resources>
监听列表点击事件:
view plain copy to clipboard print ?
- packagecn.class3g.activity;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemSelectedListener;
- importandroid.widget.Spinner;
- importandroid.widget.TextView;
- publicclassSpinnerDemoextendsActivityimplementsOnItemSelectedListener{
- SpinnersportSp=null;
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner_layout);
- findViews();
- }
- privatevoidfindViews(){
- sportSp=(Spinner)this.findViewById(R.id.sportsSp);
- sportSp.setOnItemSelectedListener(this);
- sportSp.performClick();
- }
- //每选择一次均以日志输出形式打印
- publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- TextViewtv=(TextView)arg1;
- Log.i("TAG",tv.getText().toString());
- }
- publicvoidonNothingSelected(AdapterView<?>arg0){
- }
- }
在模拟器中的效果与日志输出结果:
第二:
ListView列表视图,为列表添加列表项有两种方法,下面用一个列子分别介绍:
在样式文件中:
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="名单:"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:entries="@array/sports"
- android:divider="#00FF00"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/nameList"
- android:divider="#00FF00"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="名单:" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/sports" android:divider="#00FF00" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/nameList" android:divider="#00FF00" /> </LinearLayout>
在上面的布局中定义了两个ListView,第一个ListView通过android:entries指定了列表的项数组:
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <string-arrayname="sports">
- <item>足球</item>
- <item>篮球</item>
- <item>乒乓球</item>
- <item>网球</item>
- </string-array>
- </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="sports" > <item>足球</item> <item>篮球</item> <item>乒乓球</item> <item>网球</item> </string-array> </resources>
第二个ListView通过ArrayAdapter适配器使用数组来确定列表项,并监听点击事件,每点击用日志输出打印:
view plain copy to clipboard print ?
- packagecn.class3g.activity;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.ListView;
- publicclassListViewDemoextendsActivity
- implementsOnItemClickListener{
- ListViewnameList=null;
- String[]names={"张三","李四","王五","宋六","猪八"};
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.list_layout);
- findViews();
- }
- privatevoidfindViews(){
- nameList=(ListView)this.findViewById(R.id.nameList);
- //定义一个适配器,同时将定义列表项的数组添加进去
- ArrayAdapteradapter=newArrayAdapter(this,
- android.R.layout.simple_list_item_1,names);
- nameList.setAdapter(adapter);
- //为列表添加监听事件
- nameList.setOnItemClickListener(this);
- }
- //覆盖监听器接口OnItemClickListener的抽象方法通过日志打印所点击的列表项信息
- publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){
- Log.i("TAG",names[arg2]
- +"position="+String.valueOf(arg2)
- +"row_id="+String.valueOf(arg3));
第三:
Spinner是一个列表选择框,但其并不需要显示下拉列表,二十相当于一个菜单供用户选择,下面用一个例子介绍:
在样式文件中:
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请选择一项运动项目"
- />
- <Spinner
- android:id="@+id/sportsSp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:prompt="@string/spinner_prompt"
- android:entries="@array/sports"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请选择一项运动项目" /> <Spinner android:id ="@+id/sportsSp" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/spinner_prompt" android:entries="@array/sports" /> </LinearLayout>
在<Spinner>标签中,通过android:prompt来设置弹出选择框的标题,通过android:entries来设置默认的列表选项(定义在一个arrays.xml文件中)
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <string-arrayname="sports">
- <item>足球</item>
- <item>篮球</item>
- <item>乒乓球</item>
- <item>网球</item>
- </string-array>
- </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="sports" > <item>足球</item> <item>篮球</item> <item>乒乓球</item> <item>网球</item> </string-array> </resources>
监听列表点击事件:
view plain copy to clipboard print ?
- packagecn.class3g.activity;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemSelectedListener;
- importandroid.widget.Spinner;
- importandroid.widget.TextView;
- publicclassSpinnerDemoextendsActivityimplementsOnItemSelectedListener{
- SpinnersportSp=null;
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner_layout);
- findViews();
- }
- privatevoidfindViews(){
- sportSp=(Spinner)this.findViewById(R.id.sportsSp);
- sportSp.setOnItemSelectedListener(this);
- sportSp.performClick();
- }
- //每选择一次均以日志输出形式打印
- publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,
- longarg3){
- TextViewtv=(TextView)arg1;
- Log.i("TAG",tv.getText().toString());
- }
- publicvoidonNothingSelected(AdapterView<?>arg0){
- }
- }
package cn.class3g.activity; 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 SpinnerDemo extends Activity implements OnItemSelectedListener{ Spinner sportSp = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_layout); findViews(); } private void findViews() { sportSp = (Spinner)this.findViewById(R.id.sportsSp); sportSp.setOnItemSelectedListener(this); sportSp.performClick(); } //每选择一次均以日志输出形式打印 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { TextView tv = (TextView) arg1; Log.i("TAG", tv.getText().toString()); } public void onNothingSelected(AdapterView<?> arg0) { } }
在模拟器中的效果与日志输出结果:
更多相关文章
- Android之TextView属性列表
- Android的selector,背景选择器
- Android: 如何创建AVD以及选择合适target类型
- Android(安卓)4.0 SDK 环境搭建体验 (On Windows 7 Ultimate)
- 浅谈android的selector背景选择器
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- android的selector背景选择器
- Android(安卓)4.0 SDK 环境搭建体验(Windows 7 Ultimate)
- 浅谈android的selector,背景选择器