android ListView和ProgressBar(進(jìn)度條控件)的使用方法
ListView控件的使用:
ListView控件里面裝的是一行一行的數(shù)據(jù),一行中可能有多列,選中一行,則該行的幾列都被選中,同時(shí)可以觸發(fā)一個(gè)事件,這種控件在平時(shí)還是用得很多的。
使用ListView時(shí)主要是要設(shè)置一個(gè)適配器,適配器主要是用來(lái)放置一些數(shù)據(jù)。使用起來(lái)稍微有些復(fù)雜,這里用的是android自帶的SimpleAdapter,形式如下:
android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
由此可以看出函數(shù)的第2個(gè)參數(shù)為一個(gè)list,該list里面存放的是一些hashmap,hashmap是一些映射,里面放的是鍵值對(duì);第3個(gè)參數(shù)為1個(gè)布局文件,即適配器輸出的布局;第4個(gè)參數(shù)為字符數(shù)組,數(shù)組的內(nèi)容為參數(shù)list中map每列的列名;第5個(gè)參數(shù)為整型數(shù)組,其意思為第4個(gè)參數(shù)對(duì)應(yīng)顯示的值的格式,一般為控件。
因?yàn)榈?個(gè)參數(shù)為1個(gè)布局文件,所以我們?cè)摴こ讨形覀冃枰賳为?dú)添加一個(gè)xml文件。
同時(shí)我們要知道設(shè)置ListView的監(jiān)聽(tīng)器是用onListItemClick()函數(shù)。
另外還需注意的是在java中定義數(shù)組類型并初始化時(shí)中間不需要等號(hào),例如
new String[]{"user_name", "user_birthday"}。
這次實(shí)驗(yàn)的參考的是mars老師的資料.
ListView使用的顯示效果如下:
每次選中一行時(shí)在后臺(tái)會(huì)相應(yīng)的輸出該行的位置和id,依次選中這三行.
后臺(tái)輸出為:
實(shí)驗(yàn)代碼如下:
MainActivity.java:
package com.example.control3;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//建立一個(gè)ArrayList,ArrayList里面放的是Hash表
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String>map1 = new HashMap<String, String>();
HashMap<String, String>map2 = new HashMap<String, String>();
HashMap<String, String>map3 = new HashMap<String, String>();
//給Hash表中填入鍵值對(duì)
map1.put("user_name", "小紅");
map1.put("user_birthday", "2012_07_30");
map2.put("user_name", "小明");
map2.put("user_birthday", "2012_07_31");
map3.put("user_name", "小冬");
map3.put("user_birthday", "2012_08_01");
list.add(map1);
list.add(map2);
list.add(map3);
//在該activity中創(chuàng)建一個(gè)簡(jiǎn)單的適配器
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.activity_user, new String[]{
"user_name", "user_birthday"}, new int[]
{R.id.user_name, R.id.user_birthday});
//載入簡(jiǎn)單適配器
setListAdapter(listAdapter);
}
//ListView監(jiān)聽(tīng)器響應(yīng)函數(shù)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id--------------------" +id);
System.out.println("Position-------------" + position);
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"
/>
</LinearLayout>
</LinearLayout>
activity_user.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="horizontal"
android:padding="10dip"
>
<!--
該布局文件時(shí)水平方向上2個(gè)TextView控件,并設(shè)置了其相應(yīng)的屬性
-->
<TextView
android:id="@+id/user_name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"
/>
<TextView
android:id="@+id/user_birthday"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="10pt"
android:gravity="right"
/>
</LinearLayout>
ProgressBar控件的使用:
ProgressBar控件的使用比較簡(jiǎn)單,在android開(kāi)發(fā)中,其默認(rèn)的形式是1個(gè)圓周型的進(jìn)度條,也就是代表一直在等待,這時(shí)候是看不到實(shí)際上完成的進(jìn)度的。所以在程序中我們可以設(shè)置進(jìn)度條的形狀,比如呈水平或者垂直。界面中有1個(gè)按鈕,一開(kāi)始時(shí)是看不到進(jìn)度條的,當(dāng)按下按鈕時(shí),會(huì)彈出2個(gè)進(jìn)度條,1個(gè)呈水平狀,一個(gè)還是圓形。且這2個(gè)進(jìn)度條出現(xiàn)在按鈕的上面,為什么會(huì)這樣呢?因?yàn)檫@個(gè)activity布局中,采用的是線性布局,而那2個(gè)進(jìn)度條是放在button的前面,只是一開(kāi)始沒(méi)有設(shè)置顯示出來(lái)而已。繼續(xù)按進(jìn)度條時(shí),進(jìn)度條會(huì)前進(jìn)。且這里進(jìn)度條顯示分為主進(jìn)度條顯示和次進(jìn)度條顯示。
實(shí)驗(yàn)的結(jié)果如下:

MainActivity.java:
package com.example.control2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private int i = 0;
private ProgressBar bar1 = null;
private ProgressBar bar2 = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar1 = (ProgressBar)findViewById(R.id.bar1);
bar2 = (ProgressBar)findViewById(R.id.bar2);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new MyButtonOnClickListener());
}
class MyButtonOnClickListener implements OnClickListener{
public void onClick(View v) {
if( 0 == i )
{
bar1.setVisibility(View.VISIBLE);
bar2.setVisibility(View.VISIBLE);
}
else if( i < bar1.getMax() )
{
//設(shè)置主進(jìn)度和次進(jìn)度
bar1.setProgress(i);
bar1.setSecondaryProgress(i+10);
}
i+=10;
}
}
}
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/china"
/>
<ProgressBar
android:id="@+id/bar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/bar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
/>
</LinearLayout>
總結(jié):
通過(guò)這次實(shí)驗(yàn),對(duì)ListView和ProgressBar這2個(gè)進(jìn)度條的使用有了個(gè)初步的了解,其中ListView進(jìn)度條的使用需要用的到適配器,而視頻器的構(gòu)造稍微有點(diǎn)復(fù)雜,需要對(duì)各個(gè)參數(shù)徹底的了解。
作者:tornadomeet
相關(guān)文章
android studio 的下拉菜單Spinner使用詳解
這篇文章主要介紹了android studio 的下拉菜單Spinner使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Android開(kāi)發(fā)之天氣趨勢(shì)折線圖
在開(kāi)發(fā)天氣APP的時(shí)候會(huì)要顯示多天的信息,所以加一個(gè)折線圖來(lái)顯示一下天氣變化趨勢(shì)是很不錯(cuò)的效果,本文詳細(xì)介紹了開(kāi)發(fā)過(guò)程,下面一起來(lái)看看。2016-08-08Android筆記之:onConfigurationChanged詳解
本篇是對(duì)Android中onConfigurationChanged的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析
這篇文章主要介紹了Android中懸浮窗口的實(shí)現(xiàn)原理,以實(shí)例形式較為詳細(xì)的分析了Android懸浮窗口的原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android常用正則表達(dá)式驗(yàn)證工具類(實(shí)例代碼)
正則表達(dá)式,相信接觸過(guò)編程的人都知道,但是大部分人應(yīng)該是每次用的時(shí)候現(xiàn)找,但對(duì)其語(yǔ)法應(yīng)該只是一知半解 。下面小編給大家分享Android常用正則表達(dá)式驗(yàn)證工具類,感興趣的朋友一起看看吧2017-10-10