除了最經常用到的TextView視圖之外,還有其他一些您將頻繁使用到的基礎視圖:
● Button—表示一個按鈕的小部件。
● ImageButton—與Button視圖類似,不過它還顯示一個圖像。
● EditText—TextView視圖的子類,還允許用戶編輯其文本內容。
● CheckBox—具有兩個狀態的特殊按鈕類型:選中或未選中。
● RadioGroup和RadioButton—RadioButton有兩個狀態:選中或未選中。RadioGroup用來把一個或多個RadioButton視圖組合在一起,從而在該RadioGroup中只允許一個RadioButton被選中。
● ToggleButton—用一個燈光指示器來顯示選中/未選中狀態
下面的“試一試”揭示了這些視圖工作原理的細節。
試一試 使用基本視圖
BasicViews1.zip代碼文件可以在Wrox.com上下載
(1) 使用Eclipse創建一個Android項目,命名爲BasicView1.
(2) 在位於res/layout文件夾下的main.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="vertical" >
<Button android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="save" />
<Button android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open" />
<ImageButton android:id="@+id/btnImg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<EditText android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/chkAutosave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Autosave" />
<CheckBox android:id="@+id/star"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup android:id="@+id/rdbGp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton android:id="@+id/rdb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton android:id="@+id/rdb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
<ToggleButton android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
(3) 要觀察視圖的效果,可在Eclipse中選擇項目名稱並按F11鍵進行調試。圖4-1展示了在Android模擬器中顯示的不同視圖。
(4) 單擊不同的視圖並注意它們在外觀和感覺上的變化。圖4-2展示了視圖的以下改變:
● 第1個CheckBox視圖(Autosave)被選中。
● 第2個CheckBox視圖(星形)被選中。
● 第2個RadioButton(Option 2)被選中。
● ToggleButton被打開。
示例說明
到目前爲止,所有的視圖都是相對簡單的—使用<LinearLayout>元素將它們一一列出,因此當在活動中顯示時,它們堆疊在彼此之上。
對於第1個Button,layout_width屬性被設置爲fill_parent,因此其寬度將佔據整個屏幕的寬度:
<Button android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="save" />
對於第2個Button,layout_width屬性被設置爲wrap_content,因此其寬度將是其所包含內容的寬度—具體來說,就是顯示的文本(也就是“Open”)的寬度:
<Button android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open" />
ImageButton顯示了一個帶有圖像的按鈕。圖像通過src屬性設置。本例中,使用曾用作應用程序圖標的圖像:
<ImageButton android:id="@+id/btnImg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
EditText視圖顯示了一個矩形區域,用戶可以向其中輸入一些文本。layout_height屬性被設置爲wrap_content,這樣,如果用戶輸入一個長的文本串,EditText的高度將隨着內容自動調整(如圖4-3所示)。
<EditText android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
CheckBox顯示了一個用戶可以通過輕點鼠標進行選中或取消選中的複選框:
<CheckBox android:id="@+id/chkAutosave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Autosave" />
如果您不喜歡CheckBox的默認外觀,可以對其應用一個樣式屬性,使其顯示爲其他圖像,如星形:
<CheckBox android:id="@+id/star"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
style屬性的值的格式如下所示:
?[package:][type:]name
RadioGroup包含了兩個RadioButton。這一點很重要,因爲單選按鈕通常用來表示多個選項以便用戶選擇。當選擇了RadioGroup中的一個RadioButton時,其他所有RadioButton就自動取消選擇:
<RadioGroup android:id="@+id/rdbGp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton android:id="@+id/rdb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton android:id="@+id/rdb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
注意,RadioButton是垂直排列的,一個位於另一個之上。如果想要水平排列,需要把orientation屬性改爲horizontal。還需要確保RadioButton的layout_width屬性被設置爲wrap_content:
<RadioGroup android:id="@+id/rdbGp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton android:id="@+id/rdb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton android:id="@+id/rdb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
圖4-4顯示了水平排列的RadioButton。
ToggleButton顯示了一個矩形按鈕,用戶可以通過單擊它來實現開和關的切換:
<ToggleButton android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
這個例子中,始終保持一致的一件事情是每個視圖都有一個設置爲特定值的id屬性,如Button視圖中所示:
<Button android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/save" />
id屬性是視圖的標識符,因此可以在以後使用View.findViewById()或Activity.findViewById()方法來檢索它。
剛纔看到的各種視圖是在模擬Android 4.0智能手機的Android模擬器上進行測試的。當運行在較早版本的Android智能手機上時,它們會是什麼樣子?運行在Android平板電腦上時,又會是什麼樣子?
圖4-5顯示了將AndroidManifest.xml文件的android:minSdkVersion屬性改爲10,並在運行Android 2.3.6的Google Nexus S上運行時,活動的外觀:
<uses-sdk android:minSdkVersion="10" />
圖4-6顯示了將AndroidManifest.xml文件中的android:minSdkVersion屬性改爲13,並在運行Android 3.2.1的Asus Eee Pad Transformer上運行時,活動的外觀:
如果將android:minSdkVersion屬性設爲8或更小,然後在運行Android 3.2.1的Asus Eee Pad Transformer上運行,會看到額外的一個按鈕,如圖4-7所示。
點擊該按鈕會出現兩個選項,它們分別可以將活動拉伸到填充整個屏幕(默認選項),或將活動縮放到填充整個屏幕,如圖4-8所示。
簡言之,將最低SDK版本設爲8或更低的應用程序可以在最初設計的屏幕尺寸上顯示,也可以自動拉伸以填充屏幕(默認行爲)。
現在,您已經瞭解了一個活動的多種視圖的外觀,下面的“試一試”將教您如何以編程方式控制它們。
試一試 處理視圖事件
(1) 使用前面的“試一試”所創建的BasicViews1項目,修改BasicViews1Activity.java文件,添加下列粗體顯示的語句:
(2) 按F11鍵在Android模擬器中調試項目。
(3) 單擊不同的視圖,觀察在Toast窗口中顯示的消息。
示例說明
爲了處理每一個視圖所觸發的事件,首先需要以編程方式定位在onCreate()事件中所創建的視圖。做法是使用Acitivity基類的 findViewById()方法,傳入該視圖的ID。
//---Button view---
Button btnOpen = (Button) findViewById(R.id.btnOpen);
setOnClickListener()方法註冊了一個在視圖被單擊時調用的回調函數:
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DisplayToast("You have clicked the Open button");
}
});
當單擊視圖時,將調用onClick()方法。
對於CheckBox,爲了確定其狀態,必須把onClick()方法的參數類型轉換成一個CheckBox,然後檢查它的isChecked()方法來確定其是否被選中:
CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave);
checkBox.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if (((CheckBox)v).isChecked())
DisplayToast("CheckBox is checked");
else
DisplayToast("CheckBox is unchecked");
}
});
對於RadioButton,需要使用RadioGroup的setOnCheckedChangeListener()方法註冊一個回調函數,以便在該組中被選中的RadioButton發生變化時調用:
//---RadioButton---
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);
if (rb1.isChecked()) {
DisplayToast("Option 1 checked!");
} else {
DisplayToast("Option 2 checked!");
}
}
});
當選中一個RadioButton時,將觸發onCheckedChanged()方法。在這一過程中,找到那些單個的RadioButton,然後調用它們的isChecked()方法來確定是哪個RadioButton被選中。或者,onCheckedChanged()方法包含第2個參數,其中包含被選定RadioButton的唯一標識符。
ToggleButton的工作方式與CheckBox類似。
到目前爲止,爲了處理視圖上的事件,首先需要獲得視圖的一個引用,然後需要註冊一個回調函數來處理事件。還有另外一種處理視圖事件的方法。以Button爲例,可以向其添加一個名爲onClick的屬性:
<Button android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/save"
android:onClick="btnSaved_clicked"/>
onClick屬性指定了按鈕的單擊事件。該屬性的值就是事件處理程序的名稱。因此,爲處理按鈕的單擊事件,只需要創建一個名爲btnSaved_clicked的方法,如下面的示例所示(注意該方法必須有一個View類型的參數):
public class BasicViews1Activity extends Activity {
public void btnSaved_clicked (View view) {
DisplayToast("You have clicked the Save button1");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
}
private void DisplayToast(String msg)
{
Toast.makeText(getBaseContext(), msg,
Toast.LENGTH_SHORT).show();
}
}
如果與前面使用的方法進行比較,會發現這種方法更加簡單。使用哪種方法取決於您自己,但本書中主要使用後面這種方法。