Android Buoi 8

5
Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com TRUNG TÂM ĐÀO TẠO MẠNG MÁY TÍNH NHẤT NGHỆ ĐỐI TÁC ĐÀO TẠO CỦA MICROSOFT TẠI VIỆT NAM 105 Bà Huyện Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com Buối 8: Dialog -Exception BT1 (Demo using Dialog) Xây dựng ứng dụng tạo demo hiển thị hộp thoại

description

Bài tập android buổi 8

Transcript of Android Buoi 8

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    Bui 8: Dialog -Exception BT1 (Demo using Dialog)

    Xy dng ng dng to demo hin th hp thoi

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    HD:

    Chun b: chp 4 file delete.png, icon.png, save.png, tick.png vo trong folder drawable-hdpi

    package com.android.demo_using_dialog; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Demo_using_dialogActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnAlert = (Button) findViewById(R.id.btnAlert); Button btnAlertTwoBtns = (Button) findViewById(R.id.btnAlertWithTwoBtns); Button btnAlertThreeBtns = (Button) findViewById(R.id.btnAlertWithThreeBtns); btnAlert.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // Creating alert Dialog with one Button AlertDialog alertDialog = new AlertDialog.Builder(Demo_using_dialogActivity.this).create(); // Setting Dialog Title alertDialog.setTitle("Alert Dialog"); // Setting Dialog Message alertDialog.setMessage("Android - Nhat Nghe!"); // Setting Icon to Dialog alertDialog.setIcon(R.drawable.tick); // Setting OK Button alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to execute after dialog // closed Toast.makeText(getApplicationContext(), "Ban vua nhap OK", Toast.LENGTH_SHORT) .show(); } }); // Showing Alert Message alertDialog.show();

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    } }); /** * Showing Alert Dialog with Two Buttons one Positive Button with Label * "YES" one Negative Button with Label "NO" */ btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // Creating alert Dialog with two Buttons AlertDialog.Builder alertDialog = new AlertDialog.Builder(Demo_using_dialogActivity.this); // Setting Dialog Title alertDialog.setTitle("Confirm Delete..."); // Setting Dialog Message alertDialog.setMessage("Are you sure you want delete this?"); // Setting Icon to Dialog alertDialog.setIcon(R.drawable.delete); // Setting Positive "Yes" Button alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Ban vua nhap YES", Toast.LENGTH_SHORT).show(); } }); // Setting Negative "NO" Button alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Ban vua nhap NO", Toast.LENGTH_SHORT).show(); dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } }); /** * Showing Alert Dialog with Two Buttons one Positive Button with Label * "YES" one Neutral Button with Label "NO" one Negative Button with

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    * label "Cancel" */ btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // Creating alert Dialog with three Buttons AlertDialog.Builder alertDialog = new AlertDialog.Builder( Demo_using_dialogActivity.this); // Setting Dialog Title alertDialog.setTitle("Save File..."); // Setting Dialog Message alertDialog.setMessage("Do you want to save this file?"); // Setting Icon to Dialog alertDialog.setIcon(R.drawable.save); // Setting Positive Yes Button alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // User pressed Cancel button. Write Logic Here Toast.makeText(getApplicationContext(), "Ban vua nhap YES", Toast.LENGTH_SHORT).show(); } }); // Setting Positive Yes Button alertDialog.setNeutralButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // User pressed No button. Write Logic Here Toast.makeText(getApplicationContext(), "Ban vua nhap NO", Toast.LENGTH_SHORT) .show(); } }); // Setting Positive "Cancel" Button alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // User pressed Cancel button. Write Logic Here Toast.makeText(getApplicationContext(), "Ban vua nhap Cancel", Toast.LENGTH_SHORT).show(); }

  • Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com

    TRUNG TM O TO MNG MY TNH NHT NGH I TC O TO CA MICROSOFT TI VIT NAM 105 B Huyn Thanh Quan, Q3, TP. HCM Tel: 3.9322.735-0913.735.906 Fax: 3.9322.734 Web: nhatnghe.com

    }); // Showing Alert Message alertDialog.show(); } }); } }

    BT2 (Demo Exception)

    Vit code to demo x l ngoi l NumberFormatException( khi ngi dng khng nhp s) v ArithmeticException (khi chia cho zero) trong chng tr nh chia 2 s

    Thng bo bng hp thoi vi cc cu thng bo

    - a v b phi l 2 con s catch NumberFormatException - khng th thc hin chia cho zero catch ArithmeticException - cm n bn s dng chng trnh. finally

    BT3(Demo Exception)

    Vit code to demo x l ngoi l ArrayIndexOutOfBoundsExeption ( khi ngi dng truy xut n phn t vt ngoi phm vi mng) trong chng trnh to ra ngu nhin N s nguyn, v cho php xut ra gi tr phn t th k.

    Thng bo bng hp thoi vi cc cu thng bo

    - khng th truy xut phn t ngoi mng