文件结构
这里写图片描述

运行效果
这里写图片描述

主要代码
MainActivity

package cn.edu.sicnu.dialogdemo;

import android.app.DialogFragment;
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.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements MyInterface {

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
}public void showdialog1(View view){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("dialog 1");builder.setMessage("please choose yes or no?");builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "yes",Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("No", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show();}});builder.show();
}public void showdialog2(View view){MyDialog myDialog = new MyDialog();myDialog.show(getFragmentManager(),"dialog2");}@Override
public void buttonYesClicked() {Toast.makeText(MainActivity.this, "I am acticity , yes",Toast.LENGTH_SHORT).show();}@Override
public void buttonNoClicked() {Toast.makeText(MainActivity.this, "I am acticity , No",Toast.LENGTH_SHORT).show();}public void showdialog3(View view){AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("dialog 3");final View v =  getLayoutInflater().inflate(R.layout.dialoglayout,null);builder.setView(v);builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {EditText editText_username = v.findViewById(R.id.username);Toast.makeText(MainActivity.this, "username:"+ editText_username.getText(),Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("No", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show();}});builder.show();}

}
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
MyDialog

package cn.edu.sicnu.dialogdemo;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

public class MyDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setTitle("dialog 1");builder.setMessage("please choose yes or no?");builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {MyInterface myInterface = (MyInterface)getActivity();myInterface.buttonYesClicked();}});builder.setNegativeButton("No", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {MyInterface myInterface = (MyInterface)getActivity();myInterface.buttonNoClicked();}});return builder.create();
}

}
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
MyInterface

package cn.edu.sicnu.dialogdemo;

public interface MyInterface {
public void buttonYesClicked();
public void buttonNoClicked();

}
1
2
3
4
5
6
7
8
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.edu.sicnu.dialogdemo.MainActivity">

<Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:onClick="showdialog1"android:text="Show Dialog"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="showdialog2"android:text="Show MyDialog"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="showdialog3"android:text="Show Login"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/button2" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/button3" />

</android.support.constraint.ConstraintLayout>
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
dialoglayout.xml

<?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="match_parent">

<EditTextandroid:id="@+id/username"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="textPersonName"android:hint="username?"android:text="" />
<EditTextandroid:id="@+id/password"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:hint="password?"android:inputType="textPassword" />

</LinearLayout>

转载于:https://blog.51cto.com/10579773/2151480

Android studio Dialog 弹出式对话框相关推荐

  1. Android之AlertDialog(弹出式对话框)的使用

    一.简单的内容文本弹出式对话框 还是一样MainActivity的布局文件就不放上了,就是一个简单的Button控件,在Java代码中为其绑定了一个监听器. 首先我们需要创建这个AlertDialog ...

  2. 5弹出阴影遮罩_千文详述Cocos Creator弹出式对话框实现技术,着实硬核

    正文 在Cocos Creator游戏开发中,经常需要使用到弹出式对话框,下面我们就一起来封装下自己的弹出式对话框. 一.弹出式对话框原理解析 1:对话框的结构: 1. `根节点 -->`2. ...

  3. android studio 弹出式对话框设置时间_如何设置当单击某个对象时运行指定的应用程序?...

    在放映幻灯片的过程中,有时会需要启动其他应用程序,比如计算器或记事本来做一些辅助性的工作.在PowerPoint 2010中能否通过单击某个对象来运行指定的应用程序? 1解决方案 为指定对象设置动作效 ...

  4. android 点击图片弹出对话框,android studio怎样弹出对话框--实际案例?

    弹出对话框使用AlertDialogBuilder类构建,再用AlertDialog类具体化.我们假设在屏幕上有一退出程序的按钮,当用户点击该按钮时,弹出对话框询问是否退出程序,用户点击确定时退出程序 ...

  5. android dialog 消失动画,android 自定义dialog弹出和消失缩放动画

    本文转自:android 自定义dialog,窗口动画 Java代码: package com.sunxu.org.IndividualityDialog; import Android.app.Ac ...

  6. android dialog 动画代码,android 自定义dialog弹出和消失动画

    自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画 Java代码: package com.sunxu.org.IndividualityDialog; import ...

  7. java常用弹出式对话框

    显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", "al ...

  8. 安卓Dialog弹出对话框全解:包含了AlertDialog,DialogFragment

    全栈工程师开发手册 (作者:栾鹏) 安卓教程全解 Dialog基类中并没有定义界面,所以如果使用dialog类设置弹出框,需要使用xml自定义UI. 当然系统也自带了几个dialog派生的弹出框,例如 ...

  9. 透明设置Android:将activity设置为弹出式的并设置为透明的

    首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 1. 在res/values 下建立color.xml <resources><colorname="trans ...

最新文章

  1. 9条消除if...else的锦囊妙计,助你写出更优雅的代码
  2. 前台JS事件与服务器事件的执行顺序
  3. 解决:/bin/bash: mvn: 未找到命令
  4. Hibernate框架概述SSH框架工作原理以及流程
  5. 测试用例设计之错误推测法
  6. 最长单词(LintCode)
  7. 所有用户账户被禁用该怎么办?
  8. OJ笔记 18939 最长单词
  9. VUE项目学习(四):编写个人页面和配置路由
  10. Android 实现简单的悬浮窗按钮(一)
  11. 时间序列模型 (二):移动平均法
  12. stm32入门开发板选野火还是正点原子好,哪个的视频讲到好一点?
  13. Android应用程序未安装错误:Installation error: INSTALL_FAILED_UID_CHANGED
  14. 共聚焦显微镜能做什么
  15. 【DL】什么是dropout
  16. jq数组赋值 java_js,jquery,数组操作小结
  17. Android 检测获取 Mac 权限
  18. 计算机二级可以重复考吗,计算机二级能重复考吗?计算机二级挂了怎么办
  19. 基础时间函数总结(c语言)
  20. android dos漏洞 先知,基于goahead 的固件程序分析

热门文章

  1. 如何格式化电脑_U盘提示格式化后如何恢复数据
  2. 水磨石地面分隔条设置示意图_水磨石抛光过程中什么时候用百洁垫?什么时候用百亮钢丝棉?...
  3. 苹果开年第一购!买下仅24名员工的AI音乐公司,可根据环境动态生成音乐
  4. 中科大博士带头搬砖!这家上市公司其实是最隐秘的AI高手
  5. 擦掉纹身的AI火了:再现无暇皮肤,网友却发现“伏地魔” | Reddit热议
  6. 在Hinton看来是“宇宙答案”GPT-3,到LeCun这里却“对世界一无所知”
  7. 英国激进新冠试验曝光:招募健康志愿者,故意感染病毒,每人补偿3万5
  8. 百万奖池,鹅厂offer,2020腾讯广告算法大赛等你来战!
  9. 超过Google,微信AI在NLP领域又获一项世界第一
  10. iPhone 11的秘密武器:超宽频U1芯片,不止AirDrop,480Mbps高速传输,更有大用途