今天就跟大家聊聊有关在Android应用中使用AlertDialog对话框可以实现哪些功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
我们提供的服务有:网站制作、网站设计、微信公众号开发、网站优化、网站认证、永康ssl等。为上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的永康网站制作公司常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框
AlertDialog的常用方法
setTitle:为对话框设置标题
setMessage:为对话框设置内容
setIcon:为对话框设置图标
setItems设置对话框要显示的list
setMultiChoiceItems:一般用于复选框显示
setSingleChoiceItem:,设置单选按钮
setNeutralButton:普通按钮
setPositiveButton:添加确定按钮
setNegativeButton:添加取消按钮
setView:设置自定义样式
下面通过一个实例来了解这些方法
MainActivity.class
package com.example.alertdialog; import android.R.bool; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.content.pm.LabeledIntent; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button button1; private Button button2; private Button button3; private Button button4; private Button button5; private int mark=0; private String item[] = { "学生", "工人", "教师", "农民" }; private String multChoice[]={"旅游","电影","运动","读书"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.btn_button1); button2 = (Button) findViewById(R.id.btn_button2); button3 = (Button) findViewById(R.id.btn_button3); button4 = (Button) findViewById(R.id.btn_button4); button5 = (Button) findViewById(R.id.btn_button5); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //确认对话框 case R.id.btn_button1: { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("确认对话框"); builder.setMessage("确认退出?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你单击了确定按钮", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你单击了取消按钮", Toast.LENGTH_SHORT).show(); } }); builder.create(); builder.show(); break; } //列表对话框 case R.id.btn_button2: { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("职业"); builder.setItems(item, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你的职业是" + item[which], Toast.LENGTH_SHORT).show(); } }); builder.create(); builder.show(); break; } //多选对话框 case R.id.btn_button3: { final boolean choose[]=new boolean[multChoice.length]; AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("爱好"); builder.setMultiChoiceItems(multChoice, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub choose[which]=isChecked; } }); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String result=""; for(int i=0;i<multChoice.length;i++){ if(choose[i]){ result+=multChoice[i]+" "; } } Toast.makeText(MainActivity.this, "你的爱好["+result+"]", Toast.LENGTH_SHORT).show(); } }); builder.create(); builder.show(); break; } //单选对话框 case R.id.btn_button4: { mark=0; AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("职业"); builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub mark=which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你的职业:"+item[mark], Toast.LENGTH_SHORT).show(); } }); builder.create(); builder.show(); break; } //自定义对话框 case R.id.btn_button5: { LayoutInflater inflater=LayoutInflater.from(this); View view=inflater.inflate(R.layout.item, null); AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("自定义对话框"); builder.setView(view); builder.setNeutralButton("普通按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"我是自定义的对话框哦",Toast.LENGTH_SHORT).show(); } }); builder.create(); builder.show(); break; } } } }
文章标题:在Android应用中使用AlertDialog对话框可以实现哪些功能-创新互联
当前路径:https://www.cdcxhl.com/article0/dicpio.html
成都网站建设公司_创新互联,为您提供网站设计公司、建站公司、静态网站、App开发、移动网站建设、网站内链
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联