亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android ListView和ProgressBar(進(jìn)度條控件)的使用方法

 更新時(shí)間:2013年11月18日 15:28:16   作者:  
這篇文章主要介紹了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:

復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

<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:
復(fù)制代碼 代碼如下:

<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:
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

<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使用詳解

    這篇文章主要介紹了android studio 的下拉菜單Spinner使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Android View的事件分發(fā)詳解

    Android View的事件分發(fā)詳解

    我們?cè)趯W(xué)習(xí)View的時(shí)候,不可避免會(huì)遇到事件的分發(fā),而往往遇到的很多滑動(dòng)沖突的問(wèn)題都是由于處理事件分發(fā)時(shí)不恰當(dāng)所造成的。因此,深入了解View事件分發(fā)機(jī)制的原理,對(duì)于我們來(lái)說(shuō)是很有必要的。
    2017-12-12
  • Android開(kāi)發(fā)之天氣趨勢(shì)折線圖

    Android開(kāi)發(fā)之天氣趨勢(shì)折線圖

    在開(kāi)發(fā)天氣APP的時(shí)候會(huì)要顯示多天的信息,所以加一個(gè)折線圖來(lái)顯示一下天氣變化趨勢(shì)是很不錯(cuò)的效果,本文詳細(xì)介紹了開(kāi)發(fā)過(guò)程,下面一起來(lái)看看。
    2016-08-08
  • Android筆記之:onConfigurationChanged詳解

    Android筆記之:onConfigurationChanged詳解

    本篇是對(duì)Android中onConfigurationChanged的使用進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析

    Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析

    這篇文章主要介紹了Android中懸浮窗口的實(shí)現(xiàn)原理,以實(shí)例形式較為詳細(xì)的分析了Android懸浮窗口的原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android模擬器中安裝apk的方法

    Android模擬器中安裝apk的方法

    這篇文章主要介紹了Android模擬器中安裝apk的方法,結(jié)合圖文描述講解了Android的模擬器中實(shí)現(xiàn)安裝apk的具體步驟,簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-12-12
  • android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能

    android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能

    這篇文章主要為大家詳細(xì)介紹了android原生實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Android布局優(yōu)化之ViewStub控件

    Android布局優(yōu)化之ViewStub控件

    ViewStub是一個(gè)非常輕量級(jí)的View,這篇文章主要為大家詳細(xì)介紹了Android布局優(yōu)化之ViewStub控件的使用方法以及注意事項(xiàng),感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android常用正則表達(dá)式驗(yàn)證工具類(實(shí)例代碼)

    Android常用正則表達(dá)式驗(yàn)證工具類(實(shí)例代碼)

    正則表達(dá)式,相信接觸過(guò)編程的人都知道,但是大部分人應(yīng)該是每次用的時(shí)候現(xiàn)找,但對(duì)其語(yǔ)法應(yīng)該只是一知半解 。下面小編給大家分享Android常用正則表達(dá)式驗(yàn)證工具類,感興趣的朋友一起看看吧
    2017-10-10
  • Android源碼導(dǎo)入Eclipse步驟詳解

    Android源碼導(dǎo)入Eclipse步驟詳解

    在本文中我們給大家詳細(xì)講述了Android源碼導(dǎo)入Eclipse的步驟和具體方法,需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03

最新評(píng)論