Android入門(2) 基本控件介紹、4種佈局

一、基本控件介紹


一般新建組件有兩種方式:XML中定義和Java代碼實現,一般XML中定義較爲常用。

 

1.Button


按鈕,在main.xml中定義如下:

 

  1. <span style="font-family:'Microsoft YaHei';"><Button   
  2. <span style="WHITE-SPACE: pre"> </span>android:layout_width="wrap_content"  <!--按鈕寬度匹配文本的大小 -->  
  3.     android:layout_height="wrap_content"  <!--按鈕高度匹配文本大小 -->  
  4.     android:text="文本"   <!--按鈕的文本 -->  
  5.     android:id="@+id/button1"  <!--按鈕的id -->  
  6. ></Button></span>  

Button的監聽器:onClickListener;

需要實現方法:public void onClick(View v); v表示觸發的控件,比如按鈕

代碼示例:實現點擊按鈕生成隨機數;


ButtonActivity.java

  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2. import java.util.Random;  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. public class ButtonActivity extends Activity implements OnClickListener{    //實現點擊監聽器  
  13.     private Button button;  
  14.     private TextView tv;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         button = (Button)findViewById(R.id.button1);        //根據ID找組件  
  20.         tv = (TextView)findViewById(R.id.tv);  
  21.         button.setOnClickListener(this);        //爲button設置監聽器  
  22.     }  
  23.     @Override  
  24.     public void onClick(View view) {  
  25.         String str = new Random().nextInt()+"";  
  26.         tv.setText(str);  
  27.         Toast.makeText(this"點擊了按鈕!!", Toast.LENGTH_SHORT).show(); //設置提示信息  
  28.          Builder builder = new AlertDialog.Builder(this);           //創建對話框  
  29.          builder.setTitle("提示信息").setMessage("點擊了按鈕,隨機數爲:"+str).show(); //設置對話框屬性並顯示  
  30.     }  
  31. }</span>  


main.xml
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text=""   
  11.         android:id="@+id/tv"  
  12.         />  
  13.     <Button   
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="點擊生成隨機數"  
  17.         android:id="@+id/button1"  
  18.         ></Button>  
  19. </LinearLayout></span>  

2.ImageButton


和Button的區別爲背景可以自定義圖片,在main.xml中定義如下:

 

  1. <span style="font-family:'Microsoft YaHei';"><ImageButton   
  2.     android:layout_width="wrap_content"   
  3.     android:layout_height="wrap_content"  
  4.     android:id="@+id/ib1"  
  5.     android:background="@drawable/ic_launcher"/>  <!--設置按鈕的背景爲drawable文件夾下的ic_launcher圖片 --></span>  


代碼示例:實現點擊圖片按鈕就切換圖片;


main.xml
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.     <ImageButton   
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/ib1"  
  15.         android:background="@drawable/ic_launcher"/>  
  16. </LinearLayout></span>  

ImageButtonActivity.java

  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.widget.ImageButton;  
  9.   
  10. public class ImageButtonActivity extends Activity {  
  11.     private ImageButton ib1;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         ib1 = (ImageButton) findViewById(R.id.ib1);  
  17.         ib1.setOnTouchListener(new OnTouchListener(){  
  18.   
  19.             @Override  
  20.             public boolean onTouch(View v, MotionEvent event) {  
  21.                 if(event.getAction()==MotionEvent.ACTION_DOWN){  //按下按鈕時  
  22.                     ib1.setBackgroundResource(R.drawable.logo);  
  23.                 }  
  24.                 else if(event.getAction()==MotionEvent.ACTION_UP){  //擡起按鈕時  
  25.                     ib1.setBackgroundResource(R.drawable.ic_launcher);  
  26.                 }  
  27.                 return false;  
  28.             }  
  29.               
  30.         });  
  31.     }  
  32. }</span>  

3.EditText


文本框,在main.xml中定義如下:

  1. <span style="font-family:'Microsoft YaHei';"><EditText  
  2.      android:id="@+id/name"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:hint="輸入用戶名..."   
  6.      android:inputType=""  
  7. /></span>  

可以在<EditText>中設置以下屬性:
(1)android:inputType="number":輸入類型爲數字;

(2)android:maxLength="2":輸入最長爲2;

(3)android:singleLine="true":只能單行顯示;

(4)android:password="true" :輸入的形式爲密碼

(5)android:numeric="integer":輸入整數

代碼示例:實現用戶登錄;


  1. <p><span style="font-family:'Microsoft YaHei';"><strong>main.xml</strong>  
  2. </span></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         xmlns:android="http://schemas.android.com/apk/res/android"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="用戶名:" />  
  18.   
  19.         <EditText  
  20.             android:id="@+id/name"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:hint="輸入用戶名..."   
  24.             android:inputType=""  
  25.             />  
  26.     </LinearLayout>  
  27.   
  28.     <LinearLayout  
  29.         xmlns:android="http://schemas.android.com/apk/res/android"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:orientation="horizontal" >  
  33.  <TextView  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="密碼:" />  
  37.         <EditText  
  38.             android:id="@+id/password"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:hint="輸入密碼..."  
  42.             android:password="true" />  
  43. </LinearLayout>  
  44.         <Button  
  45.             android:id="@+id/button"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="提交" >  
  49.         </Button>  
  50.     </LinearLayout>  
  51. </span></pre>  
  52. <pre></pre>  
  53. <span style="font-family:'Microsoft YaHei'"></span><pre class="html" name="code"><p><strong><span style="font-family:'Microsoft YaHei';">EditTextActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  54.   
  55. import android.app.Activity;  
  56. import android.app.AlertDialog;  
  57. import android.app.AlertDialog.Builder;  
  58. import android.content.DialogInterface;  
  59. import android.os.Bundle;  
  60. import android.view.View;  
  61. import android.view.View.OnClickListener;  
  62. import android.widget.Button;  
  63. import android.widget.EditText;  
  64.   
  65. public class EditTextActivity extends Activity {  
  66.     private EditText name;  
  67.     private EditText password;  
  68.     private Button button;  
  69.   
  70.     @Override  
  71.     public void onCreate(Bundle savedInstanceState) {  
  72.         super.onCreate(savedInstanceState);  
  73.         setContentView(R.layout.main);  
  74.         name = (EditText) findViewById(R.id.name);  
  75.         button = (Button) findViewById(R.id.button);  
  76.         password = (EditText) findViewById(R.id.password);  
  77.         button.setOnClickListener(new OnClickListener() {  
  78.             @Override  
  79.             public void onClick(View v) {  
  80.                 String n = name.getText().toString();  
  81.                 String p = password.getText().toString();  
  82.                 Builder builder = new AlertDialog.Builder(EditTextActivity.this); // 創建對話框  
  83.                 builder.setTitle("提示信息").setMessage("用戶名:" + n + "\n密碼:" + p)  
  84.                         .setPositiveButton("知道了", new DialogInterface.OnClickListener() {  
  85.                             @Override  
  86.                             public void onClick(DialogInterface dialog, int which) {  
  87.                                 password.setText("");   //清空密碼  
  88.                             }  
  89.                         }).show();                      // 設置對話框屬性並顯示  
  90.             }  
  91.         });  
  92.     }  
  93. }</span></pre><pre class="java" name="code"><span style="font-family:微軟雅黑;"></span> </pre>  
  94. <pre></pre>  
  95. <span style="font-family:'Microsoft YaHei'"></span>  
  96. <pre></pre>  
  97. <pre></pre>  
  98. <pre></pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. </pre>  

4.CheckBox


多選框,在main.xml中定義如下:

  1. <span style="font-family:'Microsoft YaHei';"> <CheckBox  
  2.         android:id="@+id/shanghai"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:text="" /></span>  

onCheckedChangeListener監聽器是專門對CheckBox進行監聽,實現方法:public void onCheckedChanged(CompundButton buttonView,boolean isChecked);

代碼示例:實現上海、北京、天津的複選框

  1. <p><strong><span style="font-family:'Microsoft YaHei';">main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="城市:" />  
  11.   
  12.     <CheckBox  
  13.         android:id="@+id/shanghai"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="上海" />  
  17.   
  18.     <CheckBox  
  19.         android:id="@+id/beijing"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="北京" />  
  23.   
  24.     <CheckBox  
  25.         android:id="@+id/tianjing"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="天津" />  
  29.   
  30. </LinearLayout></span></pre>  
  31. <pre></pre>  
  32. <span style="font-family:'Microsoft YaHei'"></span>  
  33. <pre></pre>  
  34. <pre></pre>  
  35. <pre></pre>  
  36. <pre></pre>  
  1. <p><strong><span style="font-family:'Microsoft YaHei';">CheckBoxActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CheckBox;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.CompoundButton.OnCheckedChangeListener;  
  8. import android.widget.Toast;  
  9.   
  10. public class CheckBoxActivity extends Activity implements  
  11.         OnCheckedChangeListener {  
  12.     private CheckBox cb1, cb2, cb3;  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         cb1 = (CheckBox) findViewById(R.id.shanghai);  
  19.         cb2 = (CheckBox) findViewById(R.id.beijing);  
  20.         cb3 = (CheckBox) findViewById(R.id.tianjing);  
  21.         cb1.setOnCheckedChangeListener(this);  
  22.         cb2.setOnCheckedChangeListener(this);  
  23.         cb3.setOnCheckedChangeListener(this);  
  24.     }  
  25.   
  26.     @Override  
  27.         public void onCheckedChanged(CompoundButton buttonView,   //buttonView表示改變的框,isChecked表示是選中還是取消選中  
  28.                 boolean isChecked) {  
  29.             if(buttonView==cb1||buttonView==cb2||buttonView==cb3){  
  30.                 if(isChecked){  
  31.                     Toast.makeText(this, buttonView.getText()+"被選中",Toast.LENGTH_SHORT).show();  
  32.                 }  
  33.                 else{  
  34.                     Toast.makeText(this, buttonView.getText()+"取消選中",Toast.LENGTH_SHORT).show();  
  35.                 }  
  36.             }  
  37.           
  38.           
  39.     }  
  40. }</span></pre>  
  41. <pre></pre>  
  42. <pre></pre>  
  43. <pre></pre>  
  44. <pre></pre>  
  45. <pre></pre>  

5.RadioButton


單選框,在main.xml中定義如下:

  1. <span style="font-family:'Microsoft YaHei';"><RadioGroup>   
  2.     <RadioButton  
  3.             android:id="@+id/rb1"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="wrap_content"  
  6.             android:text="RadioButton1" >  
  7.     </RadioButton>  
  8.     <RadioButton>  
  9.     </RadioButton>  
  10.     ......  
  11. </RadioGroup></span>  

在單選框中也存在一個OnCheckedChangeListener,但是不同於多選框的監聽器,雖然名字一樣,但是所在包不一樣。

代碼示例:實現“男、女”單選框;

  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RadioGroup  
  9.         android:id="@+id/rg1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" >  
  12.   
  13.         <RadioButton  
  14.             android:id="@+id/rb1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="男" >  
  18.         </RadioButton>  
  19.   
  20.         <RadioButton  
  21.             android:id="@+id/rb2"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="女" >  
  25.         </RadioButton>  
  26.     </RadioGroup>  
  27.   
  28. </LinearLayout></span></pre>  
  29. <pre></pre>  
  30. <span style="font-family:'Microsoft YaHei'"></span>  
  31. <pre></pre>  
  32. <pre></pre>  
  33. <pre></pre>  
  34. <pre></pre>  
  1. <p><strong><span style="font-family:'Microsoft YaHei';">RadioButtonActivity.java  
  2.   
  3. </span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10. import android.widget.RadioGroup.OnCheckedChangeListener;  
  11.   
  12. public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{  
  13.     /** Called when the activity is first created. */  
  14.     private RadioButton rb1,rb2;  
  15.     private RadioGroup rg;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         rb1 = (RadioButton)findViewById(R.id.rb1);  
  21.         rb2 = (RadioButton)findViewById(R.id.rb2);  
  22.         rg = (RadioGroup)findViewById(R.id.rg1);  
  23.         rg.setOnCheckedChangeListener(this);  
  24.     }  
  25.     @Override  
  26.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  27.         if(group==rg){  
  28.             if(rb1.getId()==checkedId){  
  29.                 Toast.makeText(this, rb1.getText(), Toast.LENGTH_SHORT).show();  
  30.             }  
  31.             if(rb2.getId()==checkedId){  
  32.                 Toast.makeText(this, rb2.getText(), Toast.LENGTH_SHORT).show();  
  33.             }  
  34.         }  
  35.     }  
  36. }</span></pre>  
  37. <pre></pre>  
  38. <span style="font-family:'Microsoft YaHei'"></span>  
  39. <pre></pre>  
  40. <pre></pre>  
  41. <pre></pre>  
  42. <pre></pre>  


6.ProgressBar


進度條,在main.xml中定義如下:

  1. <span style="font-family:'Microsoft YaHei';"><ProgressBar  
  2.         android:id="@+id/pb1"  
  3.         style="?android:attr/progressBarStyleXxx"    <!--設置進度條的樣式,有大、中、小、條狀 -->  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content" /></span>  

1. ?andtroid:attr/progressBarStyleSmall圓形小進度條,動態
2. 默認,即不設置 圓形中等進度條,動態
3. ?android:attr/progressBarStyleLarge 圓形大進度條,動態
4. ?android:attr/progressBarStyleHorizontal 條狀進度條,靜態

條狀進度條屬性:


android:max
android:progress
android:secondaryProgress 

代碼示例:實現條狀進度條,並當安裝結束時,跳出提示


  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.     <ProgressBar  
  8.         android:id="@+id/pb4"  
  9.         style="?android:attr/progressBarStyleHorizontal"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:max="100"  
  13.         android:progress="0"  
  14.         android:secondaryProgress="0" />  
  15. </LinearLayout></span></pre>  
  16. <pre></pre>  
  17. <span style="font-family:'Microsoft YaHei'"></span><pre class="html" name="code"><p><strong><span style="font-family:'Microsoft YaHei';">ProgressBarActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  18.   
  19. import android.app.Activity;  
  20. import android.os.Bundle;  
  21. import android.os.Handler;  
  22. import android.widget.ProgressBar;  
  23. import android.widget.Toast;  
  24.   
  25. public class ProgressBarActivity extends Activity implements Runnable {  
  26.     private ProgressBar bar;  
  27.     private boolean isFinished;  
  28.     Thread t;  
  29.     Handler handler = new Handler();  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         bar = (ProgressBar) findViewById(R.id.pb4);  
  35.         t = new Thread(this);  
  36.         t.start();  
  37.     }  
  38.     public void showToast() {  
  39.         handler.post(new Runnable() {  
  40.             @Override  
  41.             public void run() {  
  42.                 Toast.makeText(getApplicationContext(), "安裝完成!",        //此處需要使用Handler,因爲不能在子線程中使用Toast  
  43.                         Toast.LENGTH_SHORT).show();  
  44.             }  
  45.         });  
  46.     }  
  47.     public void run() {  
  48.         int current = bar.getProgress();  
  49.         int currentMax = bar.getMax();  
  50.         int secCurrent = bar.getSecondaryProgress();  
  51.         while (true) {  
  52.             bar.setProgress(current++);  
  53.             bar.setSecondaryProgress(secCurrent++);  
  54.             if (secCurrent >= currentMax) {  
  55.                 break;  
  56.             }  
  57.             try {  
  58.                 Thread.sleep(50);  
  59.             } catch (InterruptedException e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.         isFinished = true;  
  64.         showToast();  
  65.     }  
  66. }</span></pre>  
  67. <pre></pre>  
  68. <h2><a name="t16"></a><span style="font-family:'Microsoft YaHei'">7.TextView</span></h2>  
  69. <span style="font-family:'Microsoft YaHei'">文本顯示組件,在main.xml中定義如下:</span>  
  70. <pre></pre>  
  71. <pre></pre>  
  72. <pre></pre>  
  73. <pre></pre>  
  74. <pre></pre>  
  75. <pre></pre>  
  76. <pre></pre>  
  77. <pre></pre>  
  78. </pre>  


  1. <span style="font-family:'Microsoft YaHei';"> <TextView  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/hello" />    <!--文本文字 --></span>  

8.Dialog


對話框,不需要再main.xml中顯示,只需要直接在Activity中創建即可; 

(1)簡單的Dialog:

常用函數:
setMessage()
setTitle()
setIcon()
setPositiveButton()
setNegativeButton()

  1. Builder builder = new Builder(DialogActivity.this);   //創建對話框  
  2. builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("對話框標題");   //設置對話框圖標和標題  
  3. builder.setMessage("對話框內容");    //設置對話框信息  
  4. builder.setPositiveButton("Yes"new OnClickListener(){      //設置正確按鈕  
  5.         @Override  
  6.         public void onClick(DialogInterface dialog, int arg1) {  
  7.               
  8.         }  
  9. });  
  10. builder.setNegativeButton("No"new OnClickListener(){     //設置否定按鈕  
  11.         @Override  
  12.         public void onClick(DialogInterface dialog, int arg1) {  
  13.                   
  14.         }  
  15. });  
  16. builder.show();     //顯示對話框  

(2)在dialog中添加單選框和複選框:

實例:添加“上海、北京、天津”的多選框

setMultiChoiceItems();
setSingleChoiceItems(); 
注:設置這些和setMessage不能同時使用!

  1. <pre class="java" name="code">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  7. import android.os.Bundle;  
  8.   
  9. public class DialogActivity extends Activity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         Builder builder = new Builder(DialogActivity.this);  
  14.         builder.setMultiChoiceItems(new String[] { "上海", "北京", "天津" },   //每項內容  
  15.                 new boolean[] { true, false, true },    //每項是否沒選中  
  16.                 new OnMultiChoiceClickListener() {    //監聽器  
  17.                     @Override  
  18.                     public void onClick(DialogInterface dialog, int which,  
  19.                             boolean isChecked) {  
  20.                           
  21.                     }  
  22.                 }).show();  
  23.     }  
  24. }</pre>  
  25. <pre></pre>  
  26. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  27. <div><span style="font-family:'Microsoft YaHei'"><strong>(3)在dialog中添加列表</strong></span></div>  
  28. <div><span style="font-family:'Microsoft YaHei'"><strong></strong></span></div>  
  29. <div><span style="font-family:'Microsoft YaHei'">builder.setItems(new String[]{"項1","項2"},new OnClickListener(){});  </span></div>  
  30. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  31. <div><span style="font-family:'Microsoft YaHei'"><strong>(4)在dialog中添加視圖(在main.xml中定義):</strong></span></div>  
  32. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  33. setView函數實現;<pre class="java" name="code">        Builder builder = new Builder(DialogActivity.this);  
  34.         View layout = LayoutInflater.from(this).inflate(R.layout.main, null);  
  35.         builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("對話框標題");  
  36.         builder.setMessage("對話框內容");  
  37.         builder.setPositiveButton("Yes", new OnClickListener(){  
  38.             @Override  
  39.             public void onClick(DialogInterface dialog, int arg1) {  
  40.                   
  41.             }  
  42.         });  
  43.         builder.setNegativeButton("Yes", new OnClickListener(){  
  44.             @Override  
  45.             public void onClick(DialogInterface dialog, int arg1) {  
  46.                   
  47.             }  
  48.         });  
  49.         builder.setView(layout);  
  50.         builder.show();  
  51. </pre>  
  52. <pre></pre>  
  53. <pre></pre>  
  54. <pre></pre>  
  55. <pre></pre>  

9.TabHost


分頁組件,類似於如下圖:



在main.xml中無需定義,直接在TabActivity中創建即可,但是TabSpec中的具體內容需要自定義,即引用佈局文件中的ID;
注:
(1)Activity需要繼承TabActivity 而不是Activity;
(2)OnTabChangedListener爲TabHost的監聽器,存在方法:public void onTagChanged(String tabId);

(3)TabSpec t1 = tabHost.newTabSpec("TabID");
(4)t1.setContent(佈局或控件id);   //爲tabSpec添加某個佈局
(5)t1.setIndicator(tab的標題);

代碼示例:設置三頁,每頁有各自的內容

  1. <p><strong>main.xml</strong></p><p></p><pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/l1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="第1頁"></TextView>  
  18.     </LinearLayout>  
  19.   
  20.     <LinearLayout  
  21.         android:id="@+id/l2"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:orientation="vertical" >  
  25.   
  26.         <TextView  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="第2頁"></TextView>  
  30.     </LinearLayout>  
  31.   
  32.     <LinearLayout  
  33.         android:id="@+id/l3"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:orientation="vertical" >  
  37.   
  38.         <TextView  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="第3頁"></TextView>  
  42.     </LinearLayout>  
  43.   
  44. </LinearLayout></pre>  
  45. <pre></pre>  
  46. <pre class="html" name="code"><p><strong>TabHostActivity.java</strong></p>  
  47. <pre class="java" name="code">package org.xiazdong;  
  48.   
  49. import android.app.TabActivity;  
  50. import android.os.Bundle;  
  51. import android.util.Log;  
  52. import android.view.LayoutInflater;  
  53. import android.widget.TabHost;  
  54. import android.widget.TabHost.OnTabChangeListener;  
  55. import android.widget.TabHost.TabSpec;  
  56. import android.widget.Toast;  
  57.   
  58. public class TabHostActivity extends TabActivity implements OnTabChangeListener {   //繼承TabActivity而不是Activity  
  59.     TabHost host;  
  60.   
  61.     @Override  
  62.     public void onCreate(Bundle savedInstanceState) {  
  63.         super.onCreate(savedInstanceState);  
  64.         host = this.getTabHost();    //新建TabHost  
  65.         LayoutInflater.from(this).inflate(R.layout.main,    //將main佈局文件映射成tabHost的view  
  66.                 host.getTabContentView());  
  67.         TabSpec t1 = host.newTabSpec("t1");   //新建一個頁,id爲t1  
  68.         t1.setIndicator("標籤1");  //設置顯示頁名  
  69.         t1.setContent(R.id.l1);    //設置頁的內容爲l1佈局,此處可以是佈局或組件  
  70.         host.addTab(t1);     //加入TabHost中  
  71.         TabSpec t2 = host.newTabSpec("t2");  
  72.         t2.setIndicator("標籤2",getResources().getDrawable(R.drawable.ic_launcher));  
  73.         t2.setContent(R.id.l2);  
  74.         host.addTab(t2);  
  75.         TabSpec t3 = host.newTabSpec("t3");  
  76.         t3.setIndicator("標籤3");  
  77.         t3.setContent(R.id.l3);  
  78.         host.addTab(t3);  
  79.         host.setOnTabChangedListener(this);   //設置監聽器  
  80.     }  
  81.   
  82.     @Override  
  83.     public void onTabChanged(String tabId) {  
  84.         Log.v("a","aaaa");  
  85.         if(tabId.equals("t1")){  
  86.             Toast.makeText(this, "標籤1ing", Toast.LENGTH_LONG).show();  
  87.         }  
  88.         if(tabId.equals("t2")){  
  89.             Toast.makeText(this, "標籤2ing", Toast.LENGTH_LONG).show();  
  90.         }  
  91.         if(tabId.equals("t3")){  
  92.             Toast.makeText(this, "標籤3ing", Toast.LENGTH_LONG).show();  
  93.         }  
  94.         else{  
  95.             Toast.makeText(this, tabId, Toast.LENGTH_LONG).show();  
  96.         }  
  97.     }  
  98. }</pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. <pre></pre>  
  105. <pre></pre>  
  106. <pre></pre>  
  107. <pre></pre>  
  108. </pre>  

10.SeekBar


拖動條,在main.xml中定義如下:
  1. <SeekBar   
  2. android:id="@+id/sb"  
  3. android:layout_width="fill_parent"  
  4. android:layout_height="wrap_content"  
  5. />  

注:存在OnSeekBarChangeListener監聽器,用來監聽SeekBar組件的事件,實現方法:
(1)public void onStartTrackingTouch(SeekBar seekBar); //開始移動時調用
(2)public void onStopTrackingTouch(SeekBar seekbar); //結束移動時調用
(3)public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser); //改變時調用,progress爲當前值

代碼示例:移動SeekBar組件,並在TextView中顯示當前值

  1. <p><strong>main.xml</strong></p><p></p><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello"  
  11.         android:id="@+id/tv"  
  12.          />  
  13.     <SeekBar   
  14.         android:id="@+id/sb"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.           
  18.         />  
  19. </LinearLayout></pre>  
  20. <pre></pre>  
  21. <pre class="html" name="code"><p><strong>SeekBarActivity.java</strong></p><p></p><pre class="java" name="code">package org.xiazdong;  
  22.   
  23. import android.app.Activity;  
  24. import android.os.Bundle;  
  25. import android.widget.SeekBar;  
  26. import android.widget.SeekBar.OnSeekBarChangeListener;  
  27. import android.widget.TextView;  
  28.   
  29. public class SeekBarActivity extends Activity {  
  30.     private TextView tv;  
  31.     private SeekBar sb;  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         tv = (TextView) findViewById(R.id.tv);  
  37.         sb = (SeekBar) findViewById(R.id.sb);  
  38.         sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){  
  39.             @Override  
  40.             public void onProgressChanged(SeekBar seekBar, int progress,  
  41.                     boolean fromUser) {  
  42.                 tv.setText(progress+"");  
  43.             }  
  44.             @Override  
  45.             public void onStartTrackingTouch(SeekBar seekBar) {  
  46.             }  
  47.             @Override  
  48.             public void onStopTrackingTouch(SeekBar seekBar) {  
  49.             }  
  50.         });  
  51.     }  
  52. }</pre>  
  53. <pre></pre>  
  54. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  55. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  56. <h2><a name="t22"></a><span style="font-family:'Microsoft YaHei'">11.ListView</span></h2>  
  57. <div><span style="font-family:'Microsoft YaHei'">列表視圖;</span></div>  
  58. <h3><a name="t23"></a><span style="font-family:'Microsoft YaHei'">(1)使用ArrayAdapter實現普通列表</span></h3>  
  59. <div><span style="font-family:'Microsoft YaHei'">ArrayAdapter是一個媒介,通過它可以把數組映射到ListView視圖上。</span></div>  
  60. <div>(1)new ArrayAapter<String>(this,android.R.layout.simple_list_item_1,list);   將list存放到ArrayAdapter中;</div>  
  61. <div>(2)lv.setAdapter(adapter);  爲listView設置Adapter;</div>  
  62. <div><span style="font-family:'Microsoft YaHei'"></span><pre class="java" name="code">package org.xiazdong;  
  63.   
  64. import java.util.ArrayList;  
  65.   
  66. import android.app.Activity;  
  67. import android.os.Bundle;  
  68. import android.view.View;  
  69. import android.widget.AdapterView;  
  70. import android.widget.ArrayAdapter;  
  71. import android.widget.ListView;  
  72. import android.widget.Toast;  
  73. import android.widget.AdapterView.OnItemClickListener;  
  74.   
  75. public class ListViewActivity extends Activity implements OnItemClickListener{  
  76.     ArrayList<String> list;  
  77.     @Override  
  78.     public void onCreate(Bundle savedInstanceState) {  
  79.         super.onCreate(savedInstanceState);  
  80.         list = new ArrayList<String>();  
  81.         list.add("xiazdong-1");  
  82.         list.add("xiazdong-2");  
  83.         list.add("xiazdong-3");  
  84.         ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);  
  85.         ListView lv = new ListView(this);  
  86.         lv.setAdapter(adapter);  
  87.         lv.setOnItemClickListener(this);  
  88.         this.setContentView(lv);  
  89.     }  
  90.     @Override  
  91.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  92.         Toast.makeText(this,list.get(arg2), Toast.LENGTH_SHORT).show();  
  93.     }  
  94. }</pre><br>  
  95. <h3><a name="t24"></a><span style="font-family:'Microsoft YaHei'">(2)自定義適配器BaseAdapter</span></h3>  
  96. </div>  
  97. <div><br>  
  98. </div>  
  99. <br>  
  100. <h1><a name="t25"></a><span style="font-family:'Microsoft YaHei'">二、4種佈局介紹</span></h1>  
  101. <div><span style="font-family:'Microsoft YaHei'"><br>  
  102. </span></div>  
  103. <span style="font-family:'Microsoft YaHei'">AbsoluteLayout因爲已被廢除,因此不做介紹;<br>  
  104. 只要存在界面,就會有佈局的存在,就像Swing,雖然一個是桌面應用,一個是手機應用,但是他們都差不多。<br>  
  105. </span>  
  106. <h2 style="font-family:monospace; white-space:pre"><a name="t26"></a><img alt="" src="http://images.cnblogs.com/cnblogs_com/skynet/WindowsLiveWriter/Androidview_11A60/ViewGroup%E7%9A%84%E7%BB%A7%E6%89%BF_thumb.jpg"></h2>  
  107. <br>  
  108. 此處因爲佈局非常簡單,所以就不用代碼來講解了。<br>  
  109. <br>  
  110. <br>  
  111. <h2><a name="t27"></a><span style="font-family:'Microsoft YaHei'">1.LinearLayout</span></h2>  
  112. <span style="font-family:'Microsoft YaHei'"><br>  
  113. <br>  
  114. 默認佈局。組件的排列按照預先定義方向很有序的排列,類似於Swing中的FlowLayout;<br>  
  115. 注意點:</span>  
  116. <pre></pre>  
  117. <pre></pre>  
  118. <pre></pre>  
  119. <pre></pre>  
  120. <pre></pre>  
  121. <pre></pre>  
  122. <pre></pre>  
  123. <pre></pre>  
  124. </pre>  
(1)可以在<LinearLayout>中添加android:orientation:vertical/horizontal ;
(2)可以嵌套<LinearLayout>;

2.FrameLayout


每個組件都在左上角,如果多個組件一起出現,則會重疊;

3.RelativeLayout


每個組件定位都是按照與其他組件的上下、左右定位;
默認的定位爲左上方;
(1)定位與組件的上下左右
android:layout_below="@id/.."
android:layout_above="@id/"
android:layout_toLeftOf="@id/"
android:layout_toRightOf="@id/"
(2)定位與組件的邊緣對齊
android:layout_alignLeft="@id/"
android:layout_alignRight="@id/"
android:layout_alignTop="@id/"
android:layout_alignBottom="@id/"
(3)定位與父組件的邊緣對齊
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
(4)與整個屏幕的關係
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"

4.TableLayout


類似於Swing中的GridLayout;
表格佈局的每行用<TabRow>括起來;
在<TableLayout>中可以定義如下屬性:

(1)android:shrinkColumns="1" 表明第2個控件如果裏面的內容過多,會收縮,擴展到第二行,而不是延伸;
(2)android:stretchColumns="2" 如果有空白,第3個控件填充;

在控件中設置:

(1)android:layout_column="2" 將此控件放在第3個位置;
(2)android:layout_span="2" 此控件佔據2個單元位置;

補充:


1.在Activity中根據id獲得strings.xml和main.xml中的內容


getResources().getString(int id); 
getResources().getDrawable(int id);


2.鎖定橫豎屏


因爲在CTRL+F11時 會發生問題,因此可以再AndroidManifest.xml的Activity設置:android:screenOrientation=""

(1)portrait:豎屏;
(2)landscape:橫屏;

3.可視化設置佈局、控件

main.xml 如下所示:


多個Activity之間跳轉


使用Intent進行多個頁面的跳轉;
(1)Intent intent = new Intent(Context c,Class class);  c表示當前界面,class表示要跳轉到的界面的class;
(2)intent.putExtra(String key,String value);  //設置傳輸內容;
(3)this.startActivity(intent);   //開始跳轉
(4)Intent intent = this.getIntent();  //獲得傳輸來的intent
(5)String value = intent.getStringExtra(String key);   //獲得數據

代碼示例:


main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第一個界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv1"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="輸入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b1"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="發送到第二個界面"  
  27.         />  
  28. </LinearLayout>  

mylayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第二個界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv2"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e2"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="輸入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="發送到第一個界面"  
  27.         />  
  28. </LinearLayout>  

MultiActivityActivity.java
  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.   
  12. public class MultiActivityActivity extends Activity implements OnClickListener{  
  13.     private Button b1;  
  14.     private EditText e1;  
  15.     private TextView tv1;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         b1 = (Button)findViewById(R.id.b1);  
  21.         e1 = (EditText)findViewById(R.id.e1);  
  22.         tv1 = (TextView)findViewById(R.id.tv1);  
  23.         Intent i = this.getIntent();  
  24.         if(i.getStringExtra("2")!=null){  
  25.             tv1.setText(i.getStringExtra("2"));  
  26.         }  
  27.         b1.setOnClickListener(this);  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.           
  33.         Intent intent = new Intent(MultiActivityActivity.this,OtherActivity.class);  
  34.         intent.putExtra("1", e1.getText().toString());  
  35.         this.startActivity(intent);  
  36.     }  
  37. }  

OtherActivity.java

  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class OtherActivity extends Activity implements OnClickListener{  
  14.     private TextView view ;  
  15.     private Button b2;  
  16.     private EditText e2;  
  17.     private TextView tv2;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         view = new TextView(this);  
  22.         setContentView(R.layout.mylayout);  
  23.         b2 = (Button)findViewById(R.id.b2);  
  24.         e2 = (EditText)findViewById(R.id.e2);  
  25.         tv2 = (TextView)findViewById(R.id.tv2);  
  26.         Intent i = this.getIntent();  
  27.         if(i.getStringExtra("1")!=null){  
  28.             tv2.setText(i.getStringExtra("1"));  
  29.         }  
  30.         b2.setOnClickListener(this);  
  31.     }  
  32.     @Override  
  33.     public void onClick(View v) {  
  34.           
  35.         Intent intent = new Intent(OtherActivity.this,MultiActivityActivity.class);  
  36.         intent.putExtra("2", e2.getText().toString());  
  37.         this.startActivity(intent);  
  38.     }  
  39.       
  40. }  



  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre> 
發佈了33 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章