Android中的AlertDialog和ProgressDialog用法
手机APP对话框是很多APP都有的下面来看下怎么实现的吧,
打开Android studio 然他自动创建好布局和类;
下面我们修改activity_main.xml中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" xmlns:app= "http://schemas.android.com/apk/res-auto" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <Button android:id= "@+id/btn1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Hello World!" android:layout_gravity= "center" android:layout_marginTop= "60pt" android:textAllCaps= "false" /> <Button android:id= "@+id/btn2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= "Button" android:layout_marginTop= "3pt" android:textAllCaps= "false" /> </LinearLayout> |
再activity_main.xml添加了两个按钮 分别是用于 AlertDialog 和 ProgressDialog的
下面我们再看ActivityMain中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.example.administrator.myappalertdialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.preference.DialogPreference; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mBtn1; private Button mBtn2; //声明控件 @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionbar = getSupportActionBar(); // 去除标题栏 if (actionbar!= null ) { actionbar.hide(); } mBtn1 = (Button) findViewById(R.id.btn1); //查找控件 mBtn1.setOnClickListener( this ); //创建点击事件 mBtn2 = (Button) findViewById(R.id.btn2); mBtn2.setOnClickListener( this ); } public void onClick(View view) //点击事件 { switch (view.getId()) { case R.id.btn1:AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity. this ); dialog.setTitle( "This is AlertDialog" ); //对话框标题 dialog.setMessage( "Hello world!" ); //内容 dialog.setCancelable( true ); //可撤销性 dialog.setPositiveButton( "OK" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity. this , "OK" ,Toast.LENGTH_SHORT).show(); //提示内容 } //确定按钮的点击事件 }); dialog.setNegativeButton( "Cancel" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity. this , "Cancel" ,Toast.LENGTH_SHORT).show(); //提示内容 } //取消按钮的点击事件 }); dialog.show(); //全部显示出来 break ; //下面的原理一样。 case R.id.btn2: ProgressDialog progressdialog = new ProgressDialog(MainActivity. this ); progressdialog.setTitle( "This is ProgressDialog" ); progressdialog .setMessage( "Mr.Jiang" ); progressdialog.setCancelable( false ); progressdialog.show(); break ; default : Toast.makeText(MainActivity. this , "ERROR" ,Toast.LENGTH_SHORT).show(); break ; } } } |
上面ProgressDialog 没有确定和取消按钮,我们可以把progressdialog.setCancelable里面的值改成true,按下BACK键即可退出,还有可以用progressdialog.dismiss();来去取消关闭对话框;
下面附上第一个和第二个效果图.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 7 个最近很火的开源项目「GitHub 热点速览」
· DeepSeekV3:写代码很强了
· 记一次 .NET某固高运动卡测试 卡慢分析
· Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题