Các widget sử dụng để tạo giao diện (Edit Text, Text View...) đều là lớp con của lớp View. Một số widget hay được dùng để tạo giao diện: TextView EditText Button ToggleButton CheckBox RadioButton RatingBar ListView
TextView
EditText
• Được dùng để trình bày một dòng chữ (nhãn) trên layout. • Một số thuộc tính:
• Là đối tượng nhằm cung cấp cho người sử dụng công cụ để nhập vào các giá trị.
• Một số thuộc tính:
: thiết lập cách thức hiển thị nội dung android:textStyle : kiểu thể hiện font chữ (bold, italic,...) android:textColor : màu sắc font chữ android:textSize : kích thước font chữ
OnClickListener cmdAndroidClick = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(formstuff.this, "Beep Bop", Toast.LENGTH_SHORT).show(); } };
CheckBox • Là đối tượng cung cấp cho người dùng công cụ lựa chọn hay không chọn một tiêu chí hoặc tùy chọn nào đó. kB android:id="@+id/chkAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="check it out" />
OnClickListener chkAndroidClick () { = new OnClickListener() @Override public void onClick(View v) { // TODO Auto-generated method stub if (((CheckBox)v).isChecked() == true) //checked else
//uncheck }};
Trường CĐ Công Nghệ Thông Tin TP.HCM
gg android:id="@+id/tcmdAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Vibrate On" android:textOff="Vibrate Off" /> OnClickListener tcmdAndroidClick = new OnClickListener() { @O e de @Override public void onClick(View v) { // TODO Auto-generated method stub ToggleButton _tcmdAndroid = (ToggleButton)v; if(_tcmdAndroid.isChecked() == true) //checked else //unchecked }};
RadioButton
• Tương tự CheckBox nhưng tại một thời điểm chỉ có thể 1
lựa chọn duy nhất. android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > android:id="@+id/optRed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Red" d id t t "R d" android:checked="true”/> android:id="@+id/optBlue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Blue”/> </RadioGroup>
3
Chương 3: Xây dựng giao diện cơ bản
RatingBar • Cung cấp cho người dùng công cụ đánh giá, xếp hạng. android:id="@+id/ratAndroid" android:layout_width="wrap_content"
Table Layout • Sắp xếp các thành phần con dựa trên 1 lưới các ô ngang và dọc. android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1”> <TableRow> android:layout_column="1" android:text="Open..." p g p android:padding="3dip”/> android:text="Ctrl-O" android:layout_gravity="right" android:padding="3dip”/> </TableRow> </TableLayout>
Trường CĐ Công Nghệ Thông Tin TP.HCM
Absolute Layout
• Sắp xếp các thành phần con dựa theo tọa độ x, y. của mỗi p thành phần. android:id="@+id/widget0“ android:layout_width="fill_parent“ android:layout_height="fill_parent"> android:id="@+id/widget30" android:layout_width="wrap_content" android:layout_height="wrap_content" y _x="10px" p android:layout android:layout_y="2px"/> android:id="@+id/widget31" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="30px" android:layout_y="202px"/> </AbsoluteLayout>