+44(0) 1234 567 890 info@domainname.com

Tuesday, January 22, 2013

Hướng dẫn lập trình cơ bản với Android phần 12

8:04 AM

Share it Please

B9: Việc cuối cùng cần làm là viết lại Activity. Tới Example.java và chỉnh sửa theo nội dung sau:

Mã:

package at.exam;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

public class Example extends Activity {

//Các hằng dùng cho tạo Option Menu

private static final int DELETE_WORK = Menu.FIRST;

private static final int ABOUT = Menu.FIRST + 2;

ArrayList<Work> array;

ListWorkAdapter arrayAdapter; @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

array = new ArrayList<Work>();

arrayAdapter = new ListWorkAdapter(this,

R.layout.list, array);

final EditText workEnter = (EditText) findViewById(R.id.work_enter);

final EditText hourEdit = (EditText) findViewById(R.id.hour_edit);

final EditText minuteEdit = (EditText) findViewById(R.id.minute_edit);

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

//Tạo list view cho danh sách công việc final ListView list = (ListView)

findViewById(R.id.list);

list.setAdapter(arrayAdapter);

OnClickListener add = new OnClickListener() {

@Override

public void onClick(View v) {

          if(workEnter.getText().toString().equals("") || hourEdit.getText().toString().equals("") || minuteEdit.getText().toString().equals("")) {

          AlertDialog.Builder builder = new AlertDialog.Builder(Example.this); builder.setTitle("Info missing"); builder.setMessage("Please enter all information of the work");

          builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int which) {

                    // TODO Auto-generated method stub

          }});

          builder.show();

}

else {

         String workContent = workEnter.getText().toString();

         String timeContent = hourEdit.getText().toString() + ":"+minuteEdit.getText().toString();

          Work work = new Work(workContent,timeContent);

          array.add(0, work);

          arrayAdapter.notifyDataSetChanged();

          workEnter.setText("");

         hourEdit.setText(""); minuteEdit.setText("");

}}};

button.setOnClickListener(add);

}

//Tạo Option Menu

public boolean onCreateOptionsMenu(Menu menu) {

super.onCreateOptionsMenu(menu);

menu.add(0, DELETE_WORK, 0,"Delete").setIcon(android.R.drawable.ic_delete); menu.add(0, ABOUT, 0,"About").setIcon(android.R.drawable.ic_menu_info_details); return true;

}

//Xử lý sự kiện khi các option trong Option Menu được lựa chọn

public boolean onOptionsItemSelected(MenuItem item){

switch (item.getItemId()) {

case DELETE_WORK: { deleteCheckedWork(); break;

}

case ABOUT: {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("VietAndroid"); builder.setMessage("AUTHOR:" + "\n" + " Nguyen Anh Tuan" + "\n" + "SOURCE:" + "\n" + " diendan.vietandroid.com");

builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int which) {

          }

});

          builder.setIcon(android.R.drawable.ic_dialog_info); builder.show();

         break;

}}

return true;

}

private void deleteCheckedWork() {

if (array.size() > 0) {

for (int i = 0; i < array.size(); i++) {

          if (i > array.size()) {

                break;

                     }

if (array.get(i).isChecked()) {

          array.remove(i);

          arrayAdapter.notifyDataSetChanged();

                    continue;

}

}}}}

OK. Vậy là xong. Option Menu là menu ẩn chỉ hiện ra khi bạn nhấn nút Menu của điện thoại. Option Menu rất tiện trong việc đưa ra các tùy chỉnh, giống như khi bạn nhấn phím Esc khi đang chơi game trên PC vậy.

Các bạn có thể lưu ý là thay vì sử dụng ArrayList<String> như trước mình đã thay bằng ArrayList<Work> và trong khởi tạo đối tượng arrayAdapter thì đối số thứ 2 là R.layout.list thay vì android.R.layout.simple_list_item_1, nghĩa là chúng ta đã sử dụng layout do mình tự tạo thay vì layout Android cung cấp sẵn cho hiển thị các thành phần của ListView.

Nếu chạy thử, các bạn có thể thấy khi ta đánh dấu vào checkbox của 1 thành phần trong list, rồi nhấn Menu và chọn delete thì thành phần sẽ bị gỡ bỏ khỏi danh sách.

tai lieu lap trinh android

0 comments:

Post a Comment

Tags

Thủ thuật pc Góc Làm Đẹp Làm Đẹp Da Thủ Thuật Blogspot - Wordpress - Joomla Thủ Thuật Network Giải trí Thủ Thuật Hay thu-thuat-blogspot ASP.NET Danh bạ Lập trình CSDL Thủ Thuật Blog Lập trình Thủ thuật Excel Phần Mềm Góc Phụ Nữ SEO TT Windows Lý Thuyết CCNA Dịch Vụ Hot CCNA Sức Khỏe - Đời Sống Office Thủ Thuật Mobile Thủ Thuật Win 8 Download Xây dựng liên kết Android Thủ thuật Word Website thu-thuat-internet Forum PHP Tư Vấn Làm Đẹp Mẹo Vặt tien-ich-widget Đọc báo ASP.NET MVC Ca nhạc Hỏi Đáp Thơ Vui Thủ Thuật Game vui-nhon Download Phần Mềm - Crack VBA Excel thu-thuat-facebook Cuộc sống Download Game Photoshop Thủ thuật Blogspot VBA trong Excel Widget Blogspot Địa Điểm Hot C# CSS Giới Tính Tài Liệu Đại học Chuyện lạ Clip Hài - Video Hot Góc Kỹ Năng Góc Sinh Viên Liên Minh Huyền Thoại Sao Sức Khỏe Giới Tính Thủ Thuật Tin Học template-blogger Chuyện Của Sao Design Pattern Kiến thức cơ bản PM hệ thống Thủ thuật Facebook Tin nổi bật Đề thi LAB CCNA Love story Học và hỏi Môn toán Spring.NET Tủ Sách Video lam-quen-voi-blogger Ảnh Vui Bảng Giá Con trai Cẩm nang du học Dependency Injection Hình ảnh Kinh nghiệm học Kỹ Năng Xin Việc Luật Pháp ORM Tin nóng Tiện ích Truyện cười Việt Nam hinh-anh-hai-huoc tho-vui-con-coc thu-thuat-phan-mem Blogspot Distributed Technologies Hợp Tác Kinh Doanh Khám phá MVP NHibernate Photoshop Action Thủ Thuật Win 7 Trẻ Tư Vấn Tiêu Dùng VietYeah TV Xã Hội thu-thuat-SEO Blogger Templates Game flash Khối B Nghịch Nhân vật Phong Cách Slider Thư giãn Thủ thuật Youtube WCF facebook hinh-anh-dep thu-thuat-may-tinh Đời Sống 2010 Con gái Download Game Mobile Drivers External Post Học Nấu Ăn Học vui Làm Đẹp Tóc MVC N-Tier PM hệ điều hành Sao Quốc tế Thủ thuật internet Thông Tin Tin Tức Hot Tivi Truyện Hay Tuyển Dụng Xả Stress Youtube anh-bia-facebook san-pham-google ung-dung-website Đáp án Đề thi thử AOP Abstract Factory Cool stuffs Design Principles Khối A Thông tin mạng Tình yêu lập trình viên phan-mem-van-phong thu-thuat-windows-7 thu-thuat-windows-8 Điện Tử Ảnh Nghệ Thuật 2009 2011 2012 Adapter Pattern Công Nghệ Factory Method Game Hình sự Khối D Kiếm Tiền Qua Mạng Kỹ Năng Nơi Công Sở LinQ Menu Miễn phí Photoshop PSD Sexy Singleton Sành điệu Thủ Thuật Di Động Tin nhắn SMS phan-mem-do-hoa phan-mem-tien-ich Đọc xong vọc liền .NET Remoting 10-thing series 2013 Ajax Bridge Pattern Bóng đá Castle Windsor Chơi Game Online Cover Facebook Có Thể Bạn Chưa Biết Download Phim Dịch vụ Fedora Garbage Collection Google Plus Góc Tâm Sự HTML Hibernate Hài hước IoC JSF Javascript Jquery Khối C Kỹ Năng Bán Hàng Kỹ Năng Thuyết Trình Lazy Load Linux Lock Log4Net MWC Memory Management Mock Multi-Thread MySQL Môn sử Mặc Đẹp Mẹ Và Bé Mỹ Tâm NUnit Ngọc Trinh Nhịp sống trẻ PM văn phòng POCO Passive View Photoshop tách ảnh Phạm Băng Băng Phầm Mềm Crack PostSharp Primefaces Shop Bít Tuốt Strategy Pattern Supervising Controller Sự kiện Thể thao Thủ Thuật Office Tin đặc biệt Toán Tình bạn Tình dục Tốt nghiệp Unit Testing VBA Word Virtual Server Văn WMC 2012 Windows Service acc fshare acc mien phi anh-nghe-thuat cntt di dong di động dropbox dung luong dropbox fshare hinh nen hinh nen dep hình nền job mac os mang xa hoi may ao mạng xã hội social tang dung luong dropbox thu-thuat-mobile thu-thuat-windows-xp tháng 2 top truyen-cuoi tăng dung lượng dropbox vmware Ảnh đẹp