Android中Dialog(对话框)的用法
来源:互联网 发布:数据展现工具 编辑:程序博客网 时间:2024/05/19 17:48
一、使用系统自带的Dialog布局如果要想实例化AlertDialog类往往都依靠其内部类:AlertDialog.Builder完成。
.xml <Button android:id="@+id/btn_sysdialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="系统dialog测试"/>.javapublic class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_sysdialog,btn_customdialog; private EditText name,pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_sysdialog= (Button) findViewById(R.id.btn_sysdialog); btn_sysdialog.setOnClickListener(this); btn_customdialog= (Button) findViewById(R.id.btn_customdialog); btn_customdialog.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_sysdialog: showsysdialog(); break; case R.id.btn_customdialog: showCustomDialog(); break; } } public void showsysdialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert).setTitle("标题").setMessage("是否删除?") .setPositiveButton("删除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"确认删除",Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show(); } }).setNeutralButton("查看详情", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"查看详情",Toast.LENGTH_SHORT).show(); } }); builder.create().show(); }
二、使用自定义布局
custum.xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="25sp"/> <EditText android:id="@+id/et_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入用户名" android:textSize="25sp"/> </LinearLayout>。。。。。。.java public void showCustomDialog(){ LayoutInflater inflater=LayoutInflater.from(this);//加载布局 View view=inflater.inflate(R.layout.custom_dialog,null);// 在子布局中通过ID实例化控件,必须加上加载过的view视图来调用,不然会报空指针异常 name= (EditText)view.findViewById(R.id.et_username); pwd= (EditText) view.findViewById(R.id.et_pwd); AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setView(view); builder.setTitle("登录"); builder.setPositiveButton("确认登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //在此处拿到用户输入的账号密码 String username=name.getText().toString(); String password=pwd.getText().toString(); Toast.makeText(MainActivity.this,"username:"+username+",pwd:"+password,Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.create().show(); }
0 0
- Android中Dialog(对话框)的用法
- android对话框(Dialog)的用法
- Android dialog对话框的用法
- Android中对话框(dialog)的使用
- Android中对话框(dialog)的使用
- Android 对话框(Dialog)的几种用法。
- Android 对话框(Dialog)的几种用法。
- Android对话框Dialog的基本用法
- Android Dialog对话框的一些基本用法
- Android的对话框----(Dialog)
- Android中Dialog的用法
- Android开发中Dialog对话框的使用
- Android中对话框(Dialog)的创建方法
- Android开发中Dialog对话框的使用
- Android开发中Dialog对话框的使用
- Android开发中Dialog对话框的使用
- android中控件Dialog对话框的使用
- Android中对话框(Dialog)的创建方法
- 面试感悟:3年工作经验程序员应有的技能
- STM32 USB 复合设备开发的技巧
- JVM运行时数据区域解析
- 进程与线程的区别
- JDK、tomcat环境变量的配置
- Android中Dialog(对话框)的用法
- Java:Annotation(注解)--原理到案例
- list ,set,map的特点与区别
- Struts2使用jquery ajax返回json数据
- 为什么企业宁愿开高工资给新员工,都不愿意给老员工加工资?
- rtabmap
- Eclipse构建普通的MapReduce项目
- 临时记载,后期整理
- 机器学习开发者的现代化路径:不需要从统计学微积分开始