WEB开发网
开发学院手机开发Android 开发 android 从资源中获取数组 阅读

android 从资源中获取数组

 2013-10-06 14:12:25 来源:WEB开发网   
核心提示:ListView. CHOICE_MODE_NONE:默认选择方式,如图-2所示,android 从资源中获取数组(2),ListView. CHOICE_MODE_SINGLE:单选按钮,ListView.CHOICE_MODE_MULTIPLE:复选框,8.3.3.【示例】在Activity.onCreate()方
ListView. CHOICE_MODE_NONE:默认选择方式,如图-2所示。
ListView. CHOICE_MODE_SINGLE:单选按钮。
ListView.CHOICE_MODE_MULTIPLE:复选框。
8.2.4.示例
显示图-2的列表:
图-2
步骤1、按8.1.2的步骤在res/values文件夹下创建arrays.xml文件,在该文件中定义四个直辖市的名称。
步骤2、打开res/layout/main.xml文件,在该文件中定义ListView控件,如下代码所示:
<?xml version="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">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/citys"/>
</LinearLayout>
说明:以上代码中红框中的属性指定在列表中显示res/values/arrays.xml文件中数组名为citys的数组。
8.3.OnItemClick接口
8.3.1.概述
OnItemClick接口负责监听列表项单击事件。该接口在android.widget.AdapterView包下。
8.3.2.接口中声明的方法
onItemClick(AdapterView<?> parent, View view, int position, long id)
作用:监听用户单击列表项的事件。
说明:
(1)第一个参数-parent:当前列表对象,如ListView对象。
(2)第二个参数-view:被单击的列表项对象。
(3)第三个参数-position:被单击的列表项在列表中的索引值。
(4)第四个参数-id:被单击的列表项在列表中所处的行的索引值。
大部分情况下,position与id的值相同。
8.3.3.【示例】
在Activity.onCreate()方法中调用8.2.3中定义的ListView,当单击了该列表中某项时,显示该项的标题,效果如图-3所示:
图-3
代码如下所示:
public class MainAct extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化ListView控件
ListView listView=(ListView)findViewById(R.id.listView);
//定义列表项单击事件
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,

上一页  1 2 3 4 5 6 7  下一页

Tags:android 资源 获取

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接