Lập trình Android: Alert + Toast ppt

4 173 1
Lập trình Android: Alert + Toast ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Báo động Thông thường, khi lập trình chúng ta ít khi nào đoán trước được tất cả các các lỗi xảy ra để sửa chữa. Và trong trường hợp chúng ta biết trước lỗi, chúng ta vẫn phải dùng công cụ để cảnh báo người dùng khi họ nhập sai, khi xảy ra lỗi… Và sau đây mình sẽ giới thiệu với các bạn 2 cách khác nhau để cảnh báo. Các bạn tạo Project như sau: Project name: baoDong Build Target: Android 2.3.3 Application name: baoDong Package name: com.dac.baoDong Create Activity: baoDong Kế tiếp các bạn tạo giao diện như sau: <?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" > <Button android:id="@+id/alert" android:text="Raise an alert" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/toast" android:text="Make a toast" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> Trong demo này mình chỉ làm 2 button để chạy thử 2 chế độ cảnh báo khác nhau. Tiếp theo các bạn code file BaoDong.java như sau: package com.dac.baoDong; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN import android.view.View; import android.widget.Button; import android.widget.Toast; public class BaoDong extends Activity implements View.OnClickListener { Button alert; Button toast; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); alert=(Button)findViewById(R.id.alert); alert.setOnClickListener(this); toast=(Button)findViewById(R.id.toast); toast.setOnClickListener(this); } public void onClick(View view) { if (view==alert) { new AlertDialog.Builder(this) .setTitle("MessageDemo") .setMessage("a'''!") .setNeutralButton("Close", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }) .show(); } else { Toast .makeText(this, "<hjx, hjx>", Toast.LENGTH_SHORT).show(); } } } Vậy khi ta bấm vào button Alert thì sẽ tạo ra 1 bảng Alert để cảnh báo đồng thời đóng băng chương trình đang chạy (người dùng phải bấm cancel mới có thể thực hiện lại thao tác bị lỗi, các bạn có thể thêm code khiến ứng dụng quay lại từ đầu trong hàm onClick của NeutraButton “Close”). Còn khi bấm vào button Toast thì cơ bản cũng cảnh báo tương tự như Alert nhưng Toast chỉ thể hiện 1 cảnh báo nho nhỏ và cảnh báo chỉ tồn tại trong 1 khoảng thời gian ngắn. Do đó tùy theo ứng dụng mà các bạn sử dụng các loại cảnh báo cho phù hợp. Sau đây là 1 số hình ảnh của ứng dụng: Ban đầu khi mở ứng dụng: Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN Khi bấm vào button Alert: Khi bấm vào button Toast: Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN Mọi đóng góp ý kiến các bạn vui lòng post bài vào forum www.laptrinhdidong.vn . Rất mong nhận đc sự đóng góp ý kiến của các bạn. Lập trình Android – http://laptrinhdidong.vn Page 4 . > <Button android:id="@+id /alert& quot; android:text="Raise an alert& quot; android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id /toast& quot; android:text="Make a toast& quot; android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> Trong. xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id /alert& quot; android:text="Raise. super.onCreate(savedInstanceState); setContentView(R.layout.main); alert= (Button)findViewById(R.id .alert) ; alert. setOnClickListener(this); toast= (Button)findViewById(R.id .toast) ; toast. setOnClickListener(this); } public

Ngày đăng: 08/08/2014, 03:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan