AlertDialog.Builder弹出各类窗口实现



activity_main.xml添加代码


 <TextView     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="用于显示用资源xml文件定制的AlertDialog" /> <Button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="AlertDialog.Builder弹出XML自定义Layout窗口"     android:id="@+id/btnAlertDialogWithXML"     android:onClick="onClicknAlertDialogWithXML"     android:layout_gravity="center_horizontal" /> <Button     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="没有资源的AlertDialog"     android:id="@+id/btnAlertDialog_NoRes"     android:onClick="onClickAlertNoRes"     android:layout_gravity="center_horizontal" /> <Button     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="没有资源和按钮的AlertDialog"     android:id="@+id/btnAlertDialog_NoResNoBtn"     android:onClick="onClickAlertNoResNoBtn"     android:layout_gravity="center_horizontal" />

 

创建资源文件custom_alert_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent" android:layout_height="match_parent">     <LinearLayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content">         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="警告对话框用于对客户发出警告信息,而不是用于输入信息,如果要输入信息建议用自定义对话框。自定义资源文件只是为了丰富提示信息。"             android:layout_gravity="center_horizontal" />     </LinearLayout>     <LinearLayout         android:orientation="horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content">     </LinearLayout>     <ImageView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/imageView"         android:src="@drawable/ic_launcher"         android:layout_gravity="center_horizontal" /> </LinearLayout>

 

添加MainActivity.java代码

public class MainActivity extends AppCompatActivity {     private  DialogInterface dialogWithXml;     private TextView tvAlertDialgoUserName,tvAlertDialgoPassword;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     public void onClicknAlertDialogWithXML(View view) {         LayoutInflater inflater = LayoutInflater.from(this);         View layout=inflater.inflate(R.layout.custom_alert_dialog_layout, null);         AlertDialog.Builder builder =new AlertDialog.Builder(this);         builder.setView(layout);         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {             @Override             public void onClick(DialogInterface dialog, int which) {                 dialog.cancel();             }         });         // builder.setCancelable(false);  //设置不能按返回键取消          dialogWithXml=builder.create();         builder.show();     }     public void onClickAlertNoRes(View view) {         AlertDialog.Builder myAlert=new AlertDialog.Builder(this);         myAlert.setMessage("这里是AlertDialgo信息,点击确定退出")                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface dialog, int which) {                         dialog.dismiss();                     }                 })                 .setTitle("这是标题")                 .setIcon(R.drawable.ic_launcher)                 .create();         myAlert.show();     }     public void onClickAlertNoResNoBtn(View view) {         AlertDialog.Builder myAlert=new AlertDialog.Builder(this);         myAlert.setMessage("这里是AlertDialgo信息,点击外围自动取消")                 .create();         myAlert.show();     } }

 

你可能感兴趣的:(android,AlertDialog对话框,自定义资源)