我想爲我的應用程序創建模態對話框。如何在android中創建模態對話框
所以當模態對話框打開時其他活動被阻塞。沒有事件會像後退按鈕或主頁按鈕一樣完成。
並在對話框中放兩個選項按鈕取消並確定。
謝謝...
我想爲我的應用程序創建模態對話框。如何在android中創建模態對話框
所以當模態對話框打開時其他活動被阻塞。沒有事件會像後退按鈕或主頁按鈕一樣完成。
並在對話框中放兩個選項按鈕取消並確定。
謝謝...
有許多種Android中Dialogs
。請看看Dialogs。我想你在找什麼就像AlertDialog
。這是您如何在BackPress
按鈕上執行的示例。
@Override
public void onBackPressed() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Do you want to logout?");
// alert.setMessage("Message");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Your action here
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
感謝您的代碼,但我將如何實現家庭按鍵。 –
你爲什麼想在'HOME'鍵上實現它?一些鏈接爲您的參考。 HTTP://計算器。com/questions/4783960/call-method-when-home-button-press-on-android&http://stackoverflow.com/questions/2079691/overriding-the-home-button-how-do-i-get-擺脫選擇 –
幸運的是,您無法處理「HOME」按鍵。這將允許應用程序做一些醜陋的東西。 –
試試這個::
您需要創建要在彈出窗口顯示佈局。您可以創建佈局XML和使用它像這樣:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(
layout,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
您也可以提供點擊喜歡這個按鈕的事件:
ImageButton btnChoose = (ImageButton) layout.findViewById(R.id.btnChoose);
btnChoose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
,並顯示該彈出這樣的:這裏要上顯示此按鈕點擊然後按鈕視圖將在那裏。
popupWindow.showAtLocation(anyview,Gravity.CENTER, 0, 0);
使用可以使用 setCancellable(假); setCanceledOnTouchOutside(false); 對話框本身,應該停止該對話框由BACK關閉,並通過在對話框外輕敲。
您無法覆蓋HOME按鈕。
實際上,您可以通過將應用程序設置爲「啓動器」來覆蓋主頁按鈕。 – Wildcopper
試試如下:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MyActivity.this.finish(); } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create();
Home鍵的事件:
不,這是不可能得到在Android的Home鍵事件。 在主鍵碼的文檔: http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME
公共靜態最終詮釋KEYCODE_HOME
鍵代碼常數:Home鍵。此 密鑰由框架處理,並且從未交付給 應用。
只需簡單說明一下:您可能不想硬編碼「是」和「否」字符串。相反,你可以使用像android.R.string.yes和android.R.string.no這樣的靜態資源。 –
你可以顯示你的代碼,你嘗試過嗎? –
我的問題是正確的,所以反對票給了我什麼。 –
我沒有downvote!只是要求代碼。 –