对话框有很多实现方法,最常见的是在一个点击事件中代码直接写出对话框。如下:

package com.example.lenovo.mydemo2;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
private Button mButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
//对话框标题设置
dialog.setTitle("这个是标题");
//对话框内容设置
dialog.setMessage("这个是内容");
//对话框设置不可以用Back键退出
dialog.setCancelable(false);
// dialog.clone()
/*
三种Button
Positive Button 正面按键
Negative Button 负面按键
Neutral Button 中性按键
*/
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭对话框
dialog.dismiss();
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭对话框
dialog.dismiss(); }
});
dialog.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
: }
});
//不要忘记了给对话框添加显示。
dialog.show();
}
});
}
}

运行效果:

以上就是直接在代码上实现对话框,但是这样实现对话框有一个缺点,那就是真心不好看,在实际项目中对话框与项目的主题不配套,所以我们就需要自定义实现对话框布局了:

1.我们首先需要写一个在对话框后点击效果的背景布局图片:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false">
<color android:color="@color/colorWhite"/>
</item>
<item
android:state_pressed="true">
<color android:color="@color/colorWhiteGray"/>
</item>
</selector>

2.然后需要写一个用于对话框的布局:

<?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="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_woman_default"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择添加方式"
android:textSize="@dimen/BigTextSize"
android:textColor="@color/colorBlue"
android:layout_gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/colorBlue"
android:layout_margin="10dp">
</LinearLayout>
<LinearLayout
android:id="@+id/PersonalDataModification_Dialog_CameraButton"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background_white_change_gray">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_camera"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍摄照片"
android:textSize="@dimen/BigTextSize"
android:textColor="@color/colorBlue"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/PersonalDataModification_Dialog_GalleryButton"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background_white_change_gray">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_gallery"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在相册中选择"
android:textColor="@color/colorBlue"
android:textSize="@dimen/BigTextSize"
android:layout_gravity="center"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

效果图:

3.下面是实现对话框的Java代码部分:

   public void dialogueBox(){
AlertDialog.Builder dialog = new AlertDialog.Builder(PersonalDataModification.this);
View view = LayoutInflater.from(this.getBaseContext()).inflate(R.layout.dialog_layout,null,false);
dialog.setView(view);
mDialog_CameraButton = (LinearLayout)view.findViewById(R.id.PersonalDataModification_Dialog_CameraButton);
mDialog_GalleryButton = (LinearLayout)view.findViewById(R.id.PersonalDataModification_Dialog_GalleryButton);
mDialog_CameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PersonalDataModification.this,"点了进入相机",Toast.LENGTH_SHORT).show();
}
});
mDialog_GalleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PersonalDataModification.this,"点了进入相册",Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}

将上面的方法添加到一个点击事件中,就可以实现对话框了。下面我们来看看运行效果:

android 开发 实现一个自定义布局的AlertDialog对话框的更多相关文章

  1. android 开发 写一个RecyclerView布局的聊天室,并且添加RecyclerView的点击事件

    实现思维顺序: 1.首先我们需要准备2张.9的png图片(一张图片为左边聊天泡泡,一个图片为右边的聊天泡泡),可以使用draw9patch.bat工具制作,任何图片导入到drawable中. 2.需要 ...

  2. android开发 写一个自定义形状的按键

    步骤: 1.在drawable 文件夹中创建一个xml布局文件. 2.修改布局文件 3.在需要使用背景的按键中导入布局. 创建布局文件: 修改布局文件: <?xml version=" ...

  3. android 开发 实现一个带图片Image的ListView

    注意:这种实现方法不是实现ListView的最优方法,只是希望通过练习了解ListView的实现原理 思维路线: 1.创建drawable文件夹将要使用的图片导入进去 2.写一个类,用于存放图片ID数 ...

  4. android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)

    基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...

  5. 2016 校招, Android 开发,一个本科应届的坎坷求职之路(转)

    转载出处:http://www.nowcoder.com/discuss/3244?type=2&order=0&pos=1&page=1 和大多数的面经不同,我不是大牛,手头 ...

  6. [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析

    reference to : http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法( ...

  7. .Net程序猿玩转Android开发---(3)登陆页面布局

    这一节我们来看看登陆页面如何布局.对于刚接触到Android开发的童鞋来说.Android的布局感觉比較棘手.须要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面如何布局,登陆页面非常eas ...

  8. android 开发 实现一个进入相机拍照后裁剪图片或者进入相册选中裁剪图片的功能

    实现思维路径: 以进入相机拍照的思维路线为例子: 1.进入app 2.判断之前是否保存头像,如果有就显示历史图像 (下面代码中在getOldAvatar();方法中执行这个逻辑) 3.点击更换图像的B ...

  9. android开发 RecyclerView 瀑布列表布局

    1.写一个内容的自定义小布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...

随机推荐

  1. SyntaxError: Non-ASCII character '\xe4' in file t.py on line 3, but no encoding declared

    问题 报错代码 #!/usr/bin/python s = "你好" print s 执行报错: File "t.py", line 3 SyntaxError ...

  2. cgred不能自动将pid放入tasks

    问题: cgred不能自动将pid放入tasks cgred / cgrulesengd - does not move new user pid to task file https://serve ...

  3. 【转】解决ubuntu13.10下,无法双击运行脚本文件

    解决ubuntu13.10下,无法双击运行脚本文件 转自:http://www.aichengxu.com/other/975350.htm    首先,必须先设定好脚本的运行方法,当然如果只是she ...

  4. PHP遍历一个文件夹下所有文件和子文件夹的函数

    <?php function my_dir($dir) { $files = array(); if(@$handle = opendir($dir)) { //注意这里要加一个@,不然会有wa ...

  5. docker-compose使用volume部署mysql时permission deny问题解决

    问题整体情况为使用docker做mysql的容器,然后结合其他服务一起通过docker-compose启动,并且为了一次性建表和设置用户权限我又在mysql中封装了setup.sh.schema.sq ...

  6. sqlserver 全局事务查询

    -- 此语句用于查看最老的活动事务.未完成的分布式事务或复制事务的信息. dbcc opentran -- 通过动态管理视图查看活动事务 select*from sys.dm_tran_active_ ...

  7. Web Services and C# Enums

    Web Service Transparency .NET support for web services is excellent in creating illusion of transpar ...

  8. 多线程练习,深刻体会了一次变量的BUG.

    package ltb20180106; public class TestBankThread { private int deposit=0;//注意全局变量的摆放. public TestBan ...

  9. 自动化测试Java一:Selenium入门

    From: https://blog.csdn.net/u013258415/article/details/77750214 Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介 ...

  10. sass之为什么要使用预处理器

    使用预处理器主要目的就是编写出可读性更好.更易于维护的css. 以sass为例,sass中提供了@import可以在sass文件中导入其他sass文件,或在选择器中按需导入所需要的某个属性样式: @i ...