Android常見UI組件之ListView(二)——定製ListView

來源:互聯網
上載者:User

標籤:listview   layout   xml   android   adt   

Android常見UI組件之ListView(二)——定製ListView


這一篇接上篇,展示ListView中選擇多個項及實現篩選功能~

1、在位於res/values檔案夾下的strings.xml檔案中添加如下代碼:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">BasicView5</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string><string-array name="presidents_array">    <item>Dwight D. Eisenhower</item>    <item>John F. Kennedy</item>    <item>Lyndon B. Johnson</item>    <item>Richard Nixon</item>    <item>Gerald Ford</item>    <item>Jimmy Carter</item>    <item>Ronald Reagan</item>    <item>George H.W. Bush</item>    <item>Bill Clinton</item>    <item>George W. Bush</item>    <item>Barack Obama</item></string-array></resources>


2、修改上一篇中的BasicView5.java檔案的代碼,修改後的代碼如下:

package com.example.basicview5;import android.os.Bundle;import android.app.Activity;import android.app.ListActivity;import android.view.Menu;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends ListActivity {String[] presidents;//將列表資訊儲存在strings.xml檔案中,再以編程方式讀取@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// ---no need to call this---//// setContentView(R.layout.activity_main);ListView listView = getListView();// 擷取ListActivity的列表視圖listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 可以選擇多個項listView.setTextFilterEnabled(true);//啟用篩選功能,在鍵盤上輸入,ListView自動篩選// getResources()方法以編程方式檢索與應用程式捆綁的資源presidents = getResources().getStringArray(R.array.presidents_array);setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, presidents));}public void onListItemClick(ListView parent, View v, int position, long id) {Toast.makeText(this, "You have selected " + presidents[position],Toast.LENGTH_SHORT).show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


3、運行程式,效果如下:


4、在activity_main.xml檔案中添加代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/btn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="onClick"        android:text="Show selected items" />    <ListView        android:id="@+id/android:list"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

5、在BasicView5.java檔案中添加代碼,如下:

package com.example.basicview5;import android.os.Bundle;import android.app.Activity;import android.app.ListActivity;import android.view.Menu;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends ListActivity {String[] presidents;//將列表資訊儲存在strings.xml檔案中,再以編程方式讀取@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// ---need to call this---//setContentView(R.layout.activity_main);//由於在xml檔案中添加了一個ListView部分填充一個活動,所以需要載入主動式內容。ListView listView = getListView();// 擷取ListActivity的列表視圖listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 可以選擇多個項listView.setTextFilterEnabled(true);//啟用篩選功能,在鍵盤上輸入,ListView自動篩選// getResources()方法以編程方式檢索與應用程式捆綁的資源presidents = getResources().getStringArray(R.array.presidents_array);setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, presidents));}public void onListItemClick(ListView parent, View v, int position, long id) {Toast.makeText(this, "You have selected " + presidents[position],Toast.LENGTH_SHORT).show();}public void onClick(View view) {ListView listView = getListView();String itemsSelected = "Selected items: \n";for(int i = 0; i < listView.getCount(); i++) {if(listView.isItemChecked(i)) {//找出被選中的itemitemsSelected += listView.getItemAtPosition(i) + "\n";//返回指定位置的項的名稱}}Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

6、運行效果如下:


篩選並選中幾個項,再按按鈕顯示選擇了那些項:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.