با سلام، ساخت دیالوگ برای اندروید کار خیلی ساده ایه، کدش رو من پایین گذاشتم، مواردی که لازم بود رو توضیح دادم
/////******* Sadegh Alert Dialog *********//////
// Get the layout inflater
LayoutInflater inflater = getLayoutInflater();
final AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(UserProfileActivity.this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(UserProfileActivity.this);
}
builder.setTitle("Choose Profile Picture")
.setCancelable(true)
.setMessage("How can you choose Your Profile picture?")
.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Checking camera availability
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
// capture picture
captureImage();
}
})
.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
dialog.dismiss();
return false;
}
})
.show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(UserProfileActivity.this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(UserProfileActivity.this);
}
قطعه کد بالا برای حل مشکل با ورژن های مختلف اندروید گذاشته شده.
.setCancelable(true) :
در خط بالا با true کردن این تنظیم، کاربر اگر بیرون از Dialog رو لمس کنه موجب بسته شدن دیالوگ میشه.
.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
dialog.dismiss();
return false;
}
})
در قطعه کد بالا دکمه برگشت را کنترل کرده ایم، اگر کاربر دکمه برگشت را بزنه دیالوگ بسته میشه.

مرجع: سایت AndroidDeveloper



تعداد برچسب های نامربوط به پستت خیلی زیاده برش دار هرچی غیر مرتبط به اندروید و الا گوگل به عنوان اسپمر میشنساتت و سایتت از موتور جستجو حذف میشه