“搞学习”通过精心收集,向本站投稿了8篇Android常用的五种弹出对话框,以下是小编帮大家整理后的Android常用的五种弹出对话框,欢迎大家分享。

Android常用的五种弹出对话框

篇1:Android常用的五种弹出对话框

一个Android开发中常用对话框的小例子,共有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框:

xmlns:tools=“schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

android:id=“@+id/common_dialog”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“普通对话框”

android:textSize=“16sp”

android:layout_marginTop=“10dp” />

android:id=“@+id/radio_dialog”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“单选对话框”

android:textSize=“16sp”

android:layout_marginTop=“10dp” />

android:id=“@+id/check_dialog”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“多选对话框”

android:textSize=“16sp”

android:layout_marginTop=“10dp” />

android:id=“@+id/input_dialog”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“输入文字对话框”

android:textSize=“16sp”

android:layout_marginTop=“10dp” />

android:id=“@+id/progress_dialog”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:text=“进度条对话框”

android:textSize=“16sp”

android:layout_marginTop=“10dp” />

下面是输入内容的简单布局activity_input.xml

xmlns:tools=“schemas.android.com/tools”

android:id=“@+id/LinearLayout1”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/hello_world” />

android:id=“@+id/uname”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content” />

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/hello_world” />

android:id=“@+id/upass”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content” />

代码及注释:

public class MainActivity extends Activity implements OnClickListener {

/**单选框模拟标题 大学*/

private final static int CHECKED_ENU = 0;

/**单选框模拟标题 高中*/

private final static int CHECKED_SEL = 1;

/**单选框模拟标题 初中*/

private final static int CHECKED_CHU = 2;

/**复选按钮状态为全选 */

private boolean[] checked = { true, true, true, false };

/**模拟的进度值 */

private int progressNumber;

/**进度对话框 */

private ProgressDialog progressDialog;

/**对应按钮*/

private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews;

initListeners();

}

/**初始化UI控件*/

private void initViews() {

this.commonBtn = (Button) findViewById(R.id.common_dialog);

this.radioBtn = (Button) findViewById(R.id.radio_dialog);

this.checkBtn = (Button) findViewById(R.id.check_dialog);

this.inputBtn = (Button) findViewById(R.id.input_dialog);

this.progressBtn = (Button) findViewById(R.id.progress_dialog);

}

/**注册按钮监听事件*/

private void initListeners() {

this.commonBtn.setOnClickListener(this);

this.radioBtn.setOnClickListener(this);

this.checkBtn.setOnClickListener(this);

this.inputBtn.setOnClickListener(this);

this.progressBtn.setOnClickListener(this);

}

/**普通对话框 */

private Dialog buildAlertDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle(“对话框”);

builder.setMessage(“您的密码不对!!”);

ImageView imageView = new ImageView(this);

imageView.setImageResource(R.drawable.mm1);

/**设置背景图片*/

builder.setView(imageView);

/**左边按钮*/

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是左边确定按钮!”);

}

});

/**中间按钮*/

builder.setNeutralButton(“详情”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是中间详情按钮!”);

}

});

/**右边按钮*/

builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

setTitle(“您点击的是右边取消按钮!”);

}

});

return builder.create();

}

/**单选按钮弹出框 */

private Dialog buildAlertDialog_radio() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle(“对话框”);

/**单选按钮,默认高中被选中*/

builder.setSingleChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, 1, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Android常用的五种弹出对话框// TODO Auto-generated method stub

switch (which) {

case CHECKED_ENU:

setTitle(“大学”);

break;

case CHECKED_SEL:

setTitle(“高中”);

break;

case CHECKED_CHU:

setTitle(“初中”);

break;

default:

setTitle(“小学”);

break;

}

}

});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是左边确定按钮!”);

}

});

builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是右边取消按钮!”);

}

});

return builder.create();

}

/**可以多选按钮弹出框 */

private Dialog buildAlertDialog_checkbox() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle(“对话框”);

/**复选按钮*/

builder.setMultiChoiceItems(new String[] { “大学”, “高中”, “初中”, “小学” }, checked, new DialogInterface.OnMultiChoiceClickListener() {

@Override

public void onClick(DialogInterface dialog, int which, boolean isChecked) {

setTitle(“which=” + which + “-----” + “isChecked=” + isChecked);

}

});

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击了确定按钮!”);

}

});

builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

setTitle(“您点击的是了取消按钮!”);

}

});

return builder.create();

}

/**含可以输入文本的弹出框 */

private Dialog buildAlertDialog_input() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle(“对话框”);

LayoutInflater inflater = LayoutInflater.from(this);

builder.setView(inflater.inflate(R.layout.activity_input, null));

builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是确定按钮!”);

}

});

builder.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle(“您点击的是取消按钮!”);

}

});

return builder.create();

}

/**进度对话框 */

private Dialog buildAlertDialog_progress() {

progressDialog = new ProgressDialog(this);

progressDialog.setTitle(“进度条”);

progressDialog.setMessage(“正在下载...........”);

/**进度条样式 */

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

/**模糊效果 */

progressDialog.setIndeterminate(false);

return progressDialog;

}

/**每隔0.3秒更新一次进度 */

public void updateProgress() {

new Thread() {

@Override

public void run() {

try {

while (progressNumber <= 100) {

progressDialog.setProgress(progressNumber++);

Thread.sleep(300);

super.run();

}

/**下载完后,关闭下载框 */

progressDialog.cancel();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.common_dialog:

buildAlertDialog().show();

break;

case R.id.radio_dialog:

buildAlertDialog_radio().show();

break;

case R.id.check_dialog:

buildAlertDialog_checkbox().show();

break;

case R.id.input_dialog:

buildAlertDialog_input().show();

break;

case R.id.progress_dialog:

buildAlertDialog_progress().show();

updateProgress();

break;

default:

break;

}

}

}

篇2:Android 继承DialogFragment弹出dialog对话框二

之前写过一篇关于Android 继承DialogFragment弹出dialog对话框一,这次是在上次的基础上修改了一些东西,就是怎样在DialogFragment中获取getDialog()是获取当前对话框句柄,就可以进行布局可变的灵活操作。就像getactivity();一样使用。下面看代码。

MainActivity

package com.example.fragmentdialogdemo;import com.example.fragmentdialogdemo.TestDialog.onTestListener;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends FragmentActivity implements OnClickListener, onTestListener { private String mstrName = ; private String mstrHigh = ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { Button buttonTest = (Button) findViewById(R.id.buttonTest); buttonTest.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } // 接口回调的函数 @Override public void onTestListener(int uniqueIdentifier, String strName, String strHigh) { if (uniqueIdentifier == 1) { Toast.makeText(getApplicationContext(), 姓名: + strName + ,身高: + strHigh, Toast.LENGTH_LONG) .show(); TextView textView1 = (TextView) findViewById(R.id.textView1); textView1.setText(姓名: + strName + ,身高: + strHigh); } mstrName = strName; mstrHigh = strHigh; } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.buttonTest: // 实例化TestDialog,可以传参数进去,例如标题,或者其他参数,还有一个唯一码. TestDialog dialog = new TestDialog().newInstance(请输入, 1, mstrName, mstrHigh); dialog.show(this.getSupportFragmentManager(), TestDialog); break; default: break; } }}

TestDialog

package com.example.fragmentdialogdemo;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v4.app.DialogFragment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class TestDialog extends DialogFragment implements OnCheckedChangeListener{ // mUniqueFlag作用是唯一码,可以使返回时做判断 private int mUniqueFlag = -1; private onTestListener mOnListener; private EditText meditTextName, meditTextHigh; protected Button mButtonPositive; /** * 新建实例 * * @param title * @param unique * @param strName * @param strHigh * @return */ public static TestDialog newInstance(String title, int unique, String strName, String strHigh) { TestDialog tDialog = new TestDialog(); Bundle args = new Bundle(); args.putString(SelectTemplateTitle, title); args.putInt(MultipleTemplate, unique); args.putString(TemplateName, strName); args.putString(TemplateHigh, strHigh); tDialog.setArguments(args); return tDialog; } public interface onTestListener { /** * * @param uniqueIdentifier *唯一标识 * @param strName * @param strHigh */ public abstract void onTestListener(int uniqueIdentifier, String strName, String strHigh); } // 旋转时候保存 @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(InputName, meditTextName.getText().toString()); outState.putString(InputHigh, meditTextHigh.getText().toString()); } @Override public Dialog onCreateDialog(Bundle saveInstanceState) { String title = getArguments().getString(SelectTemplateTitle); mUniqueFlag = getArguments().getInt(MultipleTemplate); AlertDialog.Builder Builder = new AlertDialog.Builder(getActivity()) .setTitle(title) .setPositiveButton(确定, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {// 触发数据回调if (mOnListener != null) mOnListener.onTestListener(mUniqueFlag, meditTextName.getText().toString(), meditTextHigh.getText().toString()); } }).setNegativeButton(取消, null); // 添加xml布局 View view = getActivity().getLayoutInflater().inflate( R.layout.test_dialog, null); setupUI(view); // 旋转后,恢复数据 if (saveInstanceState != null) { String strName = saveInstanceState.getString(InputName); if (strName != null) meditTextName.setText(strName); String strHigh = saveInstanceState.getString(InputHigh); if (strHigh != null) meditTextHigh.setText(strHigh); } Builder.setView(view); //创建对话框 AlertDialog dialog = (AlertDialog) Builder.create(); return dialog; } private void setupUI(View view) { if (view == null) return; RadioGroup radioGroup1 = (RadioGroup)view.findViewById(R.id.radioGroup1); radioGroup1.setOnCheckedChangeListener(this); String strName = getArguments().getString(TemplateName); String strHigh = getArguments().getString(TemplateHigh); meditTextName = (EditText) view.findViewById(R.id.editTextName); meditTextHigh = (EditText) view.findViewById(R.id.editTextHigh); meditTextName.setText(strName); meditTextHigh.setText(strHigh); } // onAttach是关联activity的,用接口回调 @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mOnListener = (onTestListener) activity; } catch (ClassCastException e) { dismiss(); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (group.getId() == R.id.radioGroup1) { switch (checkedId) { case R.id.radio0: //getDialog()是获取当前对话框 getDialog().findViewById(R.id.LayoutName).setVisibility(View.VISIBLE); getDialog().findViewById(R.id.LayoutHigh).setVisibility(View.VISIBLE); break; case R.id.radio1: getDialog().findViewById(R.id.LayoutName).setVisibility(View.GONE); getDialog().findViewById(R.id.LayoutHigh).setVisibility(View.VISIBLE); break; default: break; } } }}activity_main

test_dialog

二、效果看下面动态图

三、DialogFragment的部分方法

还是那句话多实践,多总结,多交流!期待你的留言,

电脑资料

篇3:android 时间对话框 TimePickerDialog详细介绍

个人在做提醒功能的时候用到了TimePickerDialog对话框,查阅了很多技术资料,但是感觉很多东西都说的不是很详细,并且很多地方,都有不完善的地方,比如有弹出对话框得到的不是系统当前的时间,而是一个其他时间的现象,让人很是苦闷,所以在经过几度研究之后,决定把个人对此空间的部分理解拿出来与大家讨论,如有不对之处,敬请谅解,会加以改进。

闲话不多说,直接进入主题:

首先,我们要在程序中声明一个日历的对象,并对其实例化获取日历实例,后面会用到。

private Calendar c = Calendar.getInstance;

实例化后便可以对该历对象进行操作了,如c.get方法可以获得该日历对象中的相关变量(如年、月、日、时、分、秒等),这些变量的值在实例化“c = Calendar.getInstance();”时已经被设置为系统默认时间;利用c.set方法可以对日历对象的相关变量进行设置。

日历对象还有一个重要的方法是setTimeInMillis,该方法只有一个参数,即距离1970年1月1日0时的毫秒数,调用这个方法,则会根据你传入的毫秒数对日历对象中的变量进行相应设置,如果想设置为当前的系统时间,则用以下方式:“c.setTimeInMillis(System.currentTimeMillis());”

下面进入主角,即时间对话框的开发。给出部分实现代码,具体的细节,自己潜心研究

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

e1 = (EditText) findViewById(R.id.c1_time);

e1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

c.setTimeInMillis(System.currentTimeMillis());

int mHour = c.get(Calendar.HOUR_OF_DAY);

int mMinute = c.get(Calendar.MINUTE);

new TimePickerDialog(ClassTimeSet.this,

new TimePickerDialog.OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker view,

int hourOfDay, int minute) {

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY, hourOfDay);

c.set(Calendar.MINUTE, minute);

c.set(Calendar.SECOND, 0); // 设为 0

c.set(Calendar.MILLISECOND, 0); // 设为 0

}

}, mHour, mMinute, true).show();

}

});

篇4:输入192.168.1.1,无法弹出用户名和密码对话框

1、检查电脑是否和LAN口(1,2,3,4口中任意一口)连好,路由器上对应的指示灯是否是亮的。

2、如果是拨号上网的用户,请先删除宽带的拨号连接,并选择“从不进行拨号连接” 。点击浏览器里面的工具---Internet选项----连接,如下图:

3、检查IE是否设置了代理,如果有请取消。在上图中点击局域网设置,如下图:

4、检查本地连接的IP地址与路由器LAN口IP处于同一网段。如果不在同一网段,可以手工配置一个IP地址,如下图:

5、如果您修改了路由器管理端口,则登陆时应输入LAN口IP:端口号。

6、重启电脑后再尝试登陆192.168.1.1的管理界面。

7、如果以上操作还是不行,可以尝试将路由器复位。

篇5:避免IE访问https弹出安全信息对话框

问:我访问一些网址开头是“”的网站或论坛时,IE老弹出“安全信息”对话框,提示“本页不但包含安全的内容,也包含不安全的内容,是否显示不安全的内容?”,每次都要点击“是”,很烦人。请问怎么才能不弹出提示?

答:打开IE,单击“工具”菜单→“Internet选项”→“安全”选项卡→“自定义级别”按钮,在弹出的“安全设置”对话框中找到“显示混合内容”,将它的设置更改为“启用”,最后单击“确定”按钮退出即可

分享到

篇6:Windows关闭安装驱动时弹出对话框问题

在XP下安装或更新设备驱动时总会弹出“欢迎使用硬件更新向导”,让选择“是,仅这一次”或“否,暂时不”,简直就是废话,可以通过设置禁用这样的废话:

展开组策略编辑器到“计算机配置/管理模板/系统”,双击“关闭Windows Update设备驱动程序搜索提示”并将该策略启用,

再展开到“计算机配置/管理模板/系统/Internet通信管理/Internet通信设置”,双击“关闭Windows Update设备驱动程序搜索”并启用该策略即可。

还可以这样去除安装设备时的废话提示:右击“我的电脑”并选择“属性/硬件/设备管理器”,在下面的“设备驱动程序搜索方式”中单击“Windows Update”,选择“从不”即可,同时还可以在“驱动程序签名”中取消加载未签名驱动时的警告框。

篇7:解决Windows关闭安装驱动时弹出对话框问题

在XP下安装或更新设备驱动时总会弹出欢迎使用硬件更新向导,让选择是,仅这一次或否,暂时不,简直就是废话,可以通过设置禁用这样的废话:

展开组策略编辑器到计算机配置/管理模板/系统,双击关闭Windows Update设备驱动程序搜索提示并将该策略启用,

再展开到计算机配置/管理模板/系统/Internet通信管理/Internet通信设置,双

击关闭Windows Update设备驱动程序搜索并启用该策略即可。

还可以这样去除安装设备时的废话提示:右击我的电脑并选择属性/硬件/设备管理器,在下面的设备驱动程序搜索方式中单击Windows Update,选择从不即可,同时还可以在驱动程序签名中取消加载未签名驱动时的警告框。

篇8:Android学习笔记(九)――更复杂的进度对话框

显示操作进度的对话框

1、使用上一篇创建的同一项目,在activity_main.xml文件中添加一个Button:

2、在MainActivity.java文件中添加具体的进度条实现代码:

首先添加onClick3()方法:

public void onClick3(View v) {

showDialog(1);// id为1,在调用回调方法onCreateDialog()时,将id传进去,使其选择case 1情况,

progressDialog.setProgress(0);// 从0开始

new Thread(new Runnable() {

@Override

public void run() {

// TODO Auto-generated method stub

for (int i = 1; i <= 10; i++) {

try {

Thread.sleep(1000);

progressDialog.incrementProgressBy(100 / 10);// 步进为10

} catch (InterruptedException e) {

// TODO: handle exception

e.printStackTrace();

}

}

progressDialog.dismiss();// 销毁对话框

}

}).start();

}

再在onCreateDialog()回调方法中添加id=1时的代码(即实现显示操作进度对话框的代码):

case 1:

progressDialog = new ProgressDialog(this);

progressDialog.setIcon(R.drawable.ic_launcher);

progressDialog.setTitle(“Downloading files...”);

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条样式

progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, “OK”,// 设置OK按钮

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), “OK clicked!”,

Toast.LENGTH_SHORT).show();

}

});

progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, “Cancel”,// 设置取消按钮

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), “Cancel clicked!”,

Toast.LENGTH_SHORT).show();

}

});

return progressDialog;

3、运行一下,效果如下:

阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。