Các control cơ bản

Một phần của tài liệu TÌM HIỂU HỆ ĐIỀU HÀNH ANDROID TẠI CÔNG TY TNHH KIẾN VIỆT (Trang 29 - 35)

2. CHƯƠNG 2: XÂY DỰNG GUI

2.2 Các control cơ bản

Trong phần trước ta đã hiểu về layout, tiếp phần này ta sẽ tìm hiểu về cách sử dụng các control cơ bản như :Button, TextView, EditText… Bảng dưới mô tả các loại control thường sử dụng trong android.

Control Type Description Related Classes

Button A push-button that can be pressed, or clicked, by the user to perform an action.

Button

Text field An editable text field. You can use theAutoCompleteTextView widget to create a text entry widget that provides auto-complete suggestions

EditText,AutoComple teTextView

Checkbox An on/off switch that can be toggled by the user. You should use checkboxes when presenting users with a group of

selectable options that are not mutually exclusive.

CheckBox

Radio button Similar to checkboxes, except that only one option can be selected in the group.

RadioGroup RadioButton

Toggle button

An on/off button with a light indicator. ToggleButton

Spinner A drop-down list that allows users to select one value from a set.

Spinner

Pickers A dialog for users to select a single value for a set by using up/down buttons or via a swipe gesture. Use aDatePickercode>

widget to enter the values for the date (month, day, year) or

a TimePicker widget to enter the values for a time (hour, minute, AM/PM), which will be formatted automatically for the user's locale.

DatePicker,TimePick er

30 2.2.1 Button

Một Button bao gồm một text hoặc một icon (hoặc cả text và icon) mà đáp ứng lại những hành

động xẩy ra khi người dùng chạm vào nó. Tùy thuộc vào mục đích của mình mà ta có thể tạo ra một button trong layout theo một trong 3 cách.

 Với text, sử dụng lớp button:

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button_text"

... />

 Với icon, sử dụng lớp button:

<ImageButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/button_icon"

... />

 Với text và icon sử dụng lớp button với thuộc tính android:drawableLeft

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button_text"

android:drawableLeft="@drawable/button_icon"

... />

Đáp ứng sử kiện OnClick trên button:

Button button = (Button) findViewById(R.id.button_send);

button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

// Do something in response to button click }

});

31 2.2.2 EditText và TextView

Trong android đối trượng EditText được sự dụng như một TextView hoặc một TextBox. Giá trị thuộc tính singleLine bằng false, EditText sẽ là một TextBox ngược lại là một TextView

<EditText

Android:id="@+id/EditText01"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:inputType="textCapWords"

Android:singleLine="true"

Android:text="Hello Android!"

Android:textColor="#000000"

Android:textSize="20dip"

Android:textStyle="bold" />

TextView là một đối tượng để hiện thị văn bản. Ta cũng có thể thay đổi cũng như lấy thuộc tính của hai đối tượng này trong mã code java. Để làm được điều này thì đầu tiên ta phải dùng hàm

TextView button = (TextView) findViewById(R.id.ten_doi_tuong);

Để lấy id của đối tượng tương ứng.

32 2.2.3 Checkboxes

Checkboxes cho phép người dùng lựa chọn một hoặc nhiều option từ một GroupCheckboxes.

Thông thường Checkboxes được sắp xếp thành một list hướng dọc.

Ví dụ:

<?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">

<CheckBox android:id="@+id/checkbox_meat"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/meat"

android:onClick="onCheckboxClicked"/>

<CheckBox android:id="@+id/checkbox_cheese"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/cheese"

android:onClick="onCheckboxClicked"/>

</LinearLayout>

public void onCheckboxClicked(View view) { // Is the view now checked?

boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked switch(view.getId()) {

case R.id.checkbox_meat:

if (checked)

// Put some meat on the sandwich else

// Remove the meat break;

case R.id.checkbox_cheese:

if (checked) // Cheese me else

// I'm lactose intolerant break;

// TODO: Veggie sandwich }

}

33 2.2.4 Radio Button

Tương tự như Checkboxes nhưng Radio Button chỉ cho phép người dùng nhập lựa chọn một option duy nhất một Group

Ví dụ:

<?xml version="1.0" encoding="utf-8"?>

<RadioGroup

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<RadioButton android:id="@+id/radio_pirates"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/pirates"

android:onClick="onRadioButtonClicked"/>

<RadioButton android:id="@+id/radio_ninjas"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/ninjas"

android:onClick="onRadioButtonClicked"/>

</RadioGroup>

sdsfdfd

a

file.java

public void onRadioButtonClicked(View view) { // Is the button now checked?

boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked switch(view.getId()) {

case R.id.radio_pirates:

if (checked)

// Pirates are the best break;

case R.id.radio_ninjas:

if (checked)

// Ninjas rule break;

} }

34 2.2.5 Toast Notification và Alert Dialog

Trong nhiều ứng dụng chúng ta phải đưa ra thông báo hay là để đưa ra một cảnh báo để người dùng xác nhận thì lúc này Toast Notification và Alert Dialog là hai đối tượng rất hữu dụng. Toast Notification chỉ đơn thuần đưa ra thông báo cho người dùng trong khoản thời gian từ 1-3s rồi tự động tắt trong khi Alert Dialog cho phép người dùng tương tác vói nó.

Để hiện thị một Toast Notification ta tạo một đối tượng thuộc Toast và cho hiện thị chúng.

// example toast Toast toast =

Toast.makeText(MainActivity.this,

"Vi du ve toast", Toast.LENGTH_LONG);

toast.show();

/*

* or Toast.makeText(MainActivity.this, "Vi du ve toast",

* Toast.LENGTH_LONG).show();

*/

Phức tạp hơn Toast Notification, Alert Dialog sử dụng phức tạp hơn một chút vì có thêm các thành phần điều khiển.

// example alert dialog

AlertDialog.Builder alertdialog = new AlertDialog.Builder(

MainActivity.this);

alertdialog.setTitle("Question");

alertdialog.setMessage("Are you sure you want to exit?");

alertdialog.setPositiveButton("Yes",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

finish();

} });

alertdialog.setNegativeButton("No",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

} });

alertdialog.create().show();

35

Một phần của tài liệu TÌM HIỂU HỆ ĐIỀU HÀNH ANDROID TẠI CÔNG TY TNHH KIẾN VIỆT (Trang 29 - 35)

Tải bản đầy đủ (PDF)

(75 trang)