也就是說,可以在layout編輯器裡新增一個表單(XML),裡可以放文字物件,EDITTEXT物件,按鈕物件,然後於AlertDialog 上顯示出來。
1.呼叫最基本的對話方塊(AlertDialog)
程式碼如下:
private void ShowAlertDialog() { Builder MyAlertDialog = new AlertDialog.Builder(this); MyAlertDialog.setTitle("我是標題"); //設定dialog顯示標題 MyAlertDialog.setMessage("顯示的內容"); //設定dialog顯示的內容 MyAlertDialog.show(); //顯示dialog }
2.如果要在對話方塊裡加入Button
程式碼如下:
private void ShowMsgDialog(String Msg) { Builder MyAlertDialog = new AlertDialog.Builder(this); MyAlertDialog.setTitle("我是標題"); MyAlertDialog.setMessage("顯示的內容"); DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //點選按鍵,所要執行的事情,如果沒有加程式碼,則會關閉dialog } }; MyAlertDialog.setNeutralButton("OK",OkClick ); //加入OK按鍵 MyAlertDialog.show(); //顯示dialog }
3.如果要在對話方塊裡加入多個按鈕
程式碼如下:
private void ShowMsgDialog(String Msg) { Builder MyAlertDialog = new AlertDialog.Builder(this); MyAlertDialog.setTitle("我是標題"); MyAlertDialog.setMessage("顯示的內容"); MyAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //按下按鈕時顯示快顯 Toast.makeText(MainActivity.this, "您按下OK按鈕", Toast.LENGTH_SHORT).show(); } }); MyAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //按下按鈕時顯示快顯 Toast.makeText(MainActivity.this, "您按下Cancel按鈕", Toast.LENGTH_SHORT).show(); } }); MyAlertDialog.show(); //顯示dialog }
4.要在對話方塊的內容套入xml的layout格式
程式碼如下:
View layout = inflater.inflate(R.layout.uninstall_noti_scollbar, null); MyAlertDialog.setView(layout); //將layout置入AlertDialog內
參考連結
參考連結