Android學習筆記(四)CheckBox、RadioGroup、ProgressBar – Android移動開發技術文章_手機開發 Android移動開發教學課程

一、多選按鈕-CheckBox
用法:首先也是通過控件ID來得到代表控件的對象,然後為其添加監聽器。
設置監聽器代碼:
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } }); <pre mce_tmp="1"><pre></pre> <p><font class="" style="FONT-FAMILY: " color="#ff7e00"> <p> 其中,swimBox是CompoundButton的子類,所以他可以向上轉型為CompoundButton的類型,將參數傳進。</p></font></p></pre>
 
 
 
 
二、單選按鈕-RadioGroup
用法:同CheckBox
設置監聽器代碼:
      genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId){ System.out.println("famale"); Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId) { System.out.println("male"); } } }); <pre mce_tmp="1"><pre></pre></pre>
 
 
 
 
 
 
 
三、進度條-ProgressBar
.xml文件中定義如下:水平進度條
<ProgressBar android:id="@+id/firstBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" //是否可視的狀態,gone是不可視的 /> <pre mce_tmp="1"><pre></pre> <p><font class="" style="FONT-FAMILY: " color="#464646"> <p> 默認的進度條最大值是100,如上例子,若想進度條自定義,則用:android:max="xxx"即可,也可以在代碼裡面設置。如:下面例子中!</p></font></p></pre>
 
 
 
 
再看一個控件
<ProgressBar android:id="@+id/secondBar" style="?android:attr/progressBarStyle" //這裡的progressBarStyle是默認的風格,就是那個一直轉的圓圈 droid:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <pre mce_tmp="1"><pre></pre> <p> </p></pre>
 
Activity中實現進度條:
          
class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { if(i == 0) { //設置進度條處於可見的狀態 firstBar.setVisibility(View.VISIBLE); firstBar.setMax(150); //代碼中設置進度條的最大值 secondBar.setVisibility(View.VISIBLE); } else if ( i < firstBar.getMax()){ //設置主進度條的當前值 firstBar.setProgress(i); //設置第二進度條的當前值 firstBar.setSecondaryProgress(i + 10); //因為默認的進度條無法顯示進行的狀態 //secondBar.setProgress(i); } else{ //設置進度條處於不可見狀態 firstBar.setVisibility(View.GONE); secondBar.setVisibility(View.GONE); } i = i + 10 ; } }
<pre mce_tmp="1"></pre>
 
 
四、列表-ListView
 
列表實現的不是Activity,而是ListActivity。
Activity代碼:
 
Java代碼 
public class Activity01 extends ListActivity { 
    /**繼承於ListActivity類*/ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        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>(); 
        map1.put("user_name", "zhangsan"); 
        map1.put("user_ip", "192.168.0.1"); 
        map2.put("user_name", "zhangsan"); 
        map2.put("user_ip", "192.168.0.2"); 
        map3.put("user_name", "wangwu"); 
        map3.put("user_ip", "192.168.0.3"); 
        list.add(map1); 
        list.add(map2); 
        list.add(map3); 
        MyAdapter listAdapter = new MyAdapter(this,  
                list,R.layout.user, new String[] { "user_name", "user_ip" }, 
                new int[] { R.id.user_name,R.id.user_ip}); 
        /**this即為當前這個ListActivity的對象,
         * list關鍵字,將鼠標放在list上可看到ArrayList<HashMap<String, String>> list
         * 表示為:首先它是一個ArrayList,裡面放上HashMap,HashMap裡又放上2個String對象
         * R.id.uder_name是佈局文件,後面一長串是數組,最後那長串是控制顯示在列表中的控件佈局
         * user_name和user_ip是對應在HashMap裡面的列,最後的new int[]則是對應在列中的值*/ 
        setListAdapter(listAdapter); 
    } 

  main.xml:
<?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"> <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="true" android:scrollbars="vertical" /> </LinearLayout> </LinearLayout>
 
 
uesr.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/user_ip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="5pt" /> </LinearLayout>

發佈留言