Android—对话框
2016-05-12 16:25
417 查看
layout文件:
java类代码:
效果图:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hanqi.testapp2.TestActivity5" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一般对话框" android:onClick="bt1_onClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bt2_onClick" android:text="单选对话框"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bt3_onClick" android:text="复选对话框"/> </LinearLayout>
java类代码:
package com.hanqi.testapp2; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class TestActivity5 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test5); } //一般对话框 public void bt1_onClick(View v) { //对话框不能直接实例化 //内部提供了构造器 //方法链调用 AlertDialog alertDialog = new AlertDialog.Builder(this) .setTitle("确认对话框") .setMessage("确实要删除么") .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "执行删除,which = " + which, Toast.LENGTH_SHORT).show(); } })//正面按钮 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "取消删除,which = " + which, Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "按钮,which = " + which, Toast.LENGTH_SHORT).show(); } }).setCancelable(false) .show();//显示对话框 } //单选对话框 public void bt2_onClick(View v) { //final可以让常量的生命周期延长到整个实例 final String [] str = {"男","女"}; new AlertDialog.Builder(this) .setTitle("单选对话框") .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "which = "+which+ ",选中的是"+str[which], Toast.LENGTH_SHORT).show(); //关闭对话框 dialog.dismiss(); } }).setCancelable(false) .show(); } //复选对话框 public void bt3_onClick(View v) { final String [] str = {"宝马","奔驰","劳斯莱斯","宾利"}; final boolean[] ch = {true,false,false,true}; new AlertDialog.Builder(this) .setTitle("复选对话框") .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //改变对应数组项的选中状态 ch[which] = isChecked; } }) .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int i = 0; //获取最终的选中状态 for (boolean b:ch) { Toast.makeText(TestActivity5.this, str[i]+"选中状态 = "+ (b?"选中":"未选中"), Toast.LENGTH_SHORT).show(); i++; } } }) .setNegativeButton("取消", null) .setCancelable(false) .show(); } }
效果图:
相关文章推荐
- Android 内功心法(1.9)——java设计模式之装饰模式
- Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理
- Android 常见错误
- 【Android 】Dalvik虚拟机和JVM虚拟机的区别
- Android读取系统mp3文件
- android波浪背景
- android binder机制
- 自定义AlertDialog
- android自定义控件的最大高度MaxHeightView
- Android:SQLite无法update/insert/delete数据(数据库被locked)
- Android Volley框架使用源码分享
- 为Android添加开机启动脚本
- Android仿斗鱼领取鱼丸文字验证(二)
- Android开发自定义控件实现一个球赛胜负数统计条
- 通过Glide回调获取Bitmap对象并转换成Base64
- 12. Android框架和工具之 StringUtils(字符串操作)
- 【Android】 Android之API类库整理
- 11. Android框架和工具之 Logger(调试代码)
- Android 开发中遇到的Error:Execution failed for task ': :dexDebug'. > com.android.ide.common.process.Proces
- android 获取手机包名显示