Sunday, July 3, 2016

Cách tạo Alert dialog trong android



package com.example.macbookpro.dtsonalertdialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
        b.setTitle("Thoat ung dung");
        b.setMessage("Ban co muon thoat ung dung khong?");
        b.setPositiveButton("yes", new DialogInterface.OnClickListener(){

            @Override            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        b.create().show();

    }
}

Ý nghĩa các hàm trong Alert Dialog:
– setMessage: Thiết lập nội dung cho Dialog "ban co muon thoat ung dung"
– setTitle : thiết lập tiêu đề cho Dialog "Thoat ung dung"
– setIcon : để thiết lập Icon hình hiển thị cho Alert dialog
– setPositiveButtonsetNegativeButton thiết lập sự kiện click cho nut các nút khi hiển thi ngoài Alert dialog
– create() để tạo Dialog
– show() để hiển thị Dialog.

No comments:

Post a Comment