1.代码运行目录
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.android"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity" /> 12 <activity android:name=".TestActivity4" /> 13 <activity android:name=".Main2Activity" /> 14 <activity android:name=".Main3Activity"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 <category android:name="android.intent.category.LAUNCHER" /> 18 </intent-filter> 19 </activity> 20 <activity android:name=".Main5Activity"></activity> 21 </application> 22 23 </manifest>
2.布局文件
activity_mani.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.administrator.android.Main3Activity" 7 android:orientation="vertical"> 8 <!--对话框--> 9 <Button 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text="一般对话框" 13 android:onClick="bt1_onclick"/> 14 <Button 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="单选对话框" 18 android:onClick="bt2_onclick"/> 19 <Button 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="复选对话框" 23 android:onClick="bt3_onclick"/> 24 <Button 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:text="自定义对话框" 28 android:onClick="bt4_onclick"/> 29 <Button 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:text="登录对话框" 33 android:onClick="bt5_onclick"/> 34 <Button 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:text="日期对话框" 38 android:onClick="bt6_onclick"/> 39 <Button 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" 42 android:text="时间对话框" 43 android:onClick="bt7_onclick"/> 44 <Button 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:text="普通进度对话框" 48 android:onClick="bt8_onclick"/> 49 </LinearLayout>
截屏如下:
3.各种对话框的运行JAVA代码。
Mani3Activity.java
第一种:一般对话框
1 package com.example.administrator.android; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.Dialog; 6 import android.app.ProgressDialog; 7 import android.app.TimePickerDialog; 8 import android.content.DialogInterface; 9 import android.support.v7.app.AppCompatActivity; 10 import android.os.Bundle; 11 import android.text.Layout; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.widget.DatePicker; 15 import android.widget.EditText; 16 import android.widget.TimePicker; 17 import android.widget.Toast; 18 import java.util.Calendar; 19 20 public class Main3Activity extends AppCompatActivity { 21 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main3); 27 } 28 public void bt1_onclick(View v) { 29 //一般对话框 不能直接实例化 30 //内部提供构造器 31 //方法链调用 32 AlertDialog alertDialog = new AlertDialog.Builder(this) 33 .setTitle("确认对话框") 34 .setMessage("您确实要删除吗?") 35 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 36 @Override 37 public void onClick(DialogInterface dialog, int which) { 38 Toast.makeText(Main3Activity.this, "取消删除……,which = " + which, Toast.LENGTH_SHORT).show(); 39 } 40 })//负面按钮 41 42 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 43 @Override 44 public void onClick(DialogInterface dialog, int which) { 45 Toast.makeText(Main3Activity.this, "执行删除……,which = " + which, Toast.LENGTH_SHORT).show(); 46 } 47 })//正面按钮 48 49 .setNeutralButton("中立", new DialogInterface.OnClickListener() { 50 @Override 51 public void onClick(DialogInterface dialog, int which) { 52 Toast.makeText(Main3Activity.this, "执行命令……,which = " + which, Toast.LENGTH_SHORT).show(); 53 } 54 })//普通按钮 55 .setCancelable(false) //杜绝点击其他位置关闭对话框 不能以分号结尾 56 .show(); //显示对话框的方法 57 } 58 }
第二种:单选对话框
1 package com.example.administrator.android; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.Dialog; 6 import android.app.ProgressDialog; 7 import android.app.TimePickerDialog; 8 import android.content.DialogInterface; 9 import android.support.v7.app.AppCompatActivity; 10 import android.os.Bundle; 11 import android.text.Layout; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.widget.DatePicker; 15 import android.widget.EditText; 16 import android.widget.TimePicker; 17 import android.widget.Toast; 18 import java.util.Calendar; 19 20 public class Main3Activity extends AppCompatActivity { 21 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main3); 27 } 28 //单选对话框 29 public void bt2_onclick(View v) { 30 //final,中态 延长常量生命周期 31 final String[] str = {"男", "女"}; 32 final AlertDialog a = new AlertDialog.Builder(this) 33 .setTitle("单选对话框") 34 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { 35 @Override 36 public void onClick(DialogInterface dialog, int which) { 37 Toast.makeText(Main3Activity.this, "which = " + which + 38 ",选中的是:" + str[which], Toast.LENGTH_LONG).show(); 39 //关闭对话框 40 dialog.dismiss(); 41 } 42 }) 43 .setCancelable(false) //模态窗口 44 .show(); 45 } 46 }