警告對話框用於提示用戶要求決定或者一些信息。例如:有時你可能想問從用戶的響應將帶他們到另一個事件。所以,這可以在Android開發AlertDialogBuilder的幫助下完成的。讓我們了解如何實現警告對話框在Android的幫助下一個例子:
- 1步:首先你需要創建一個Android應用程序在你的Android工作室在一個包中。在這裏,我們將使用我們的新計劃:
com.firstapp.ashulakhwan.greatlearning
- 2步:現在,你需要修改MainActivity。java文件。在這個文件中,您需要添加警告對話框的代碼。您還可以參考下麵的代碼:
包com.firstapp.ashulakhwan.greatlearning;進口android.app.AlertDialog;進口android.content.DialogInterface;進口android.support.v7.app.ActionBarActivity;進口android.os.Bundle;進口android.view.View;進口android.widget.Toast;公開課MainActivity延伸ActionBarActivity {@Override空白onCreate(包savedInstanceState){超級保護。onCreate (savedInstanceState);setContentView(出來。activity_main);}公共無效(查看視圖){AlertDialog開放。新AlertDialog Builder alertDialogBuilder =。構建器(這); alertDialogBuilder.setMessage ("Choose Yes, if you want to open another window and No if you want to stay on this window."); alertDialogBuilder.setPositiveButton ("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this,"Now, you will be taken to another window in this app.",Toast.LENGTH_LONG).show(); } }); alertDialogBuilder.setNegativeButton ("No",new DialogInterface.OnClickListener() { Override public void onClick (DialogInterface dialog, int which) { finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create (); alertDialog.show(); } }
- 3步:修改MainActivity之後。java文件,您需要修改activity_main另一個文件。xml可以添加任何GUI組件的地方。下麵的代碼片段:
< ?xml version = " 1.0 " encoding = " utf - 8 " ?> < RelativeLayout xmlns: android = " http://schemas.android.com/apk/res/android " xmlns:工具= " http://schemas.android.com/tools " android: layout_width =“match_parent”android: layout_height =“match_parent”android: paddingLeft =“@dimen / activity_horizontal_margin”android: paddingRight =“@dimen / activity_horizontal_margin”android: paddingTop =“@dimen / activity_vertical_margin”android: paddingBottom =“@dimen / activity_vertical_margin”工具:上下文= "。MainActivity " > < TextView android: layout_width = " wrap_content“android: layout_height =“wrap_content”android:文本=“大beplay2018官网學習”android: id =“@ + id / textView2”android:輸入textColor = " # FF5733 " android: textSize = 40 dp“android: layout_below =“@ + id / TextView”android: layout_centerHorizontal = " true " / > < ImageView android: layout_width =“wrap_content”android: layout_height =“wrap_content”android: id =“@ + id / ImageView”android: layout_below =“@ + id / textView2”android: layout_alignRight =“@ + id / textView2”android: layout_alignEnd =“@ + id / textView2”android: layout_alignLeft =“@ + id / TextView”android: layout_alignStart =“@ + id / TextView”/ > <按鈕android: layout_width =“wrap_content”android: layout_height =“wrap_content”android:文本=“警告對話框”android: id =“@ + id /按鈕”android: layout_below =“@ + id / ImageView”android: layout_alignRight =“@ + id / textView2”android: layout_alignEnd =“@ + id / textView2”android: layout_marginTop = 50 dp“android: onClick =“開放”android: layout_alignLeft =“@ + id / ImageView”android: layout_alignStart =“@ + id / ImageView”/ > < / RelativeLayout >
- 4步:最後,你不得不在Android模擬器上運行應用程序或一個Android設備。
現在,如果您遵循了前麵的所有步驟,您可以運行應用程序,看看它是如何工作的。