1. 程式人生 > >android的常用控制元件總結【安卓入門五】

android的常用控制元件總結【安卓入門五】

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。                   
                         RadioButton單選按鈕控制元件的使用方法
==================================================================================
1、RadioButton在main.xml中的佈局
  

 <RadioGroup
       android:id="@+id/genderGroup"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
    >
       <RaioButton
            android:id="@+id/maleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
        />
        <Button
            android:id="@+id/famleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
         />
    </RaioGroup>


2、//宣告成員變數
 

  private RadioGroup radioGroup = null;
   private RadioButton maleRadioButton = null;
   private RadioButton femaleRadioButton = null;


3、在onCreate(Bundle savedInstanceState){
      

  radioGroup = (RadioGroup)findViewById(R.id.genderGroup);
        maleRadioButton = (RadioButton)findViewById(R.id.maleButton);
        famaleRadioButton = (RadioButton)findViewById(R.id.famaleButton);
        //監聽處理,內部類去實現
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener          (){
               public void onCheckedChanged(RadioGroup group,int checkedId){
                  if(famaleRadioButton.getId()==checkedId){
                     System.out.println("famaleButton is checked!");
                     //toast彈出訊息框
                     Toast.makeText(當前類.this,"famale",Toast.LENGTH_SHORT).show();
                  }
                  else if(maleRadioButton.getId()==checkedId){
                      System.out.println("male is checked!");
                      Toast.makeText(當前類.this,"male",Toast.LENGTH_SHORT).show();
                  }
                }
            }
        );
     }


==================================================================================、。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                              CheckBox多選框的使用方法
==================================================================================

   //CheckBox的使用方法,不存在組的概念

1、在main.xml檔案中佈局
  

<CheckBox
      android:id="@+id/swin"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="游泳"
    />


2、//宣告成員變數
  

private CheckBox swinBox = null;
   swinBox = (CheckBox)findViewById(R.id.swin);


3、設定監聽,用匿名內部類的方法
 

  swinBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         public void onCheckedChange(CompoundButton buttonView,boolean isChecked){
             if(isChecked){
                System.out.println("swin is checked");
                Toast.makeText(當前類.this,"swin",Toast.LENGTH_SHORT).show();
             }
         }
     }
   );


==================================================================================
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                           ProgressBar進度條控制元件
==================================================================================
1、android中的控制元件ProgressBar中:
   
  

android:visibili="gone"表示進度條不可視


2、//android的ProgressBar的水平佈局
   style="?android:attr/progressBarStyleHorizontal"
==================================================================================
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                         Spinner下拉選單控制元件的使用方法
===================================================================================
1、Spinner佈局標籤形式
  

 <Spinner
       android:id="@+id/spinnerld"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
   />


2、在string.xml當中宣告一個數組:
  

 <string-arry name="planets_array">
      <item>Mercury</item>
      <item>Venus</item>
      <item>Earth</item>
      <item>Mars</item>
      <item>Jupiter</item>
      <item>Saturn</item>
      <item>Uranus</item>
      <item>Nepturn</item>
   </string-arry>


3、建立一個ArrayAdapter:
   //定義下拉選單的樣子
 

  ArrayAdapter<CharSequence> adapter = 
        ArrayAdapter.createFromResource(               
                   this,                  
                   R.array.splanets_array,
                   android.R.layout.simple_spinner_item);
                   ); 
      //設定Spinner的樣式,引用android系統提供的佈局檔案     
      adapter.setDropDownViewResource(
                   android.R.layout.simple_spinner_dropdown_item);


4、得到Spinner物件,並設定資料
   
  

spinner = (Spinner)findViewById(R.id.spinnerld);
   spinner.setAdapter(adapter);
   spinner.setPrompt("測試");


5、建立一個監聽器,繫結在一起
  

spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener());


6、監聽器中的方法
  

SpinnerOnSelectedListener implements OnItemSelectedListener{
       @override
       onItemSelected(AdapterView<?> adapterView,View view,int position,long id){
             String selected = adapterView.getItemAtPosition(position).toString();
             System.out.println(selected);
       }

       @override
       onNothingSelected(AdapterView<?> adapterView){
             System.out.println("nothingSelected");    
       }
   }


===================================================================================
  ArrayAdapter的另一種用法:動態的建立ArrayAdapter

1、建立item.xml佈局檔案

2、

List<String> list = new ArrayList<String>();
   list.add("test1");
   list.add("test2");
   ArrayAdapter adapter = new 
        ArrayAdapter(this,R.layout.item,R.id.textViewld,list);