4 使用AlertDialog实现提示框

4-1 认识Dialog (04:11)

一、Dialog
对话框是在当前界面弹出的一个小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等。一般情况下,影虎要与对话框进行交互,然后返回到被只改的界面以继续运行当前的应用程序。



二、AlertDialog
要创建一个AlertDialog,就要用到AlertDialog.Builder的create()方法。
setTitle:为对话框设置标题
setIcon:为对话框设置图标
setMessage:为对话框设置内容
setView:给对话框设置自定义样式
setItems:设置对话框要显示的一个list,一般用于显示几个命令时
setMultiChoiceItems:用来设置对话框显示一系列的复选框
setSingleChoiceItems:设置单选按钮
setNeutralButton:普通按钮
setPositiveButton:给对话框添加“确认”按钮
setNegativeButton:给对话框添加“取消”按钮

 

三、使用AlterDialog创建对话框的大致步骤:

1.创建AlertDialog.Builder 对象,该对象是AlertDialog的创建器

2.调用AlertDialog.Builder 的方法为对话框设置图标、标题、内容等

3.调用AlertDialog.Builder 的create()方法创建AlertDialog对话框

4.调用AlertDialogshow()方法显示对话框。

MainActivity.javapackage com.imooc.dialog;import android.app.Activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    String [] single_list = {"男","女","女博士","程序员"};
    String [] multi_list = {"篮球","足球","男生","女生"};
    String [] item_list = {"项目经理","策划","测试","美工","程序猿"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEvent();
    }

    /**
     * 初始化点击事件
     */
    private void initEvent() {
        findViewById(R.id.dialog_btn1).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog1();
                    }
                });
        findViewById(R.id.dialog_btn2).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog2();
                    }
                });
        findViewById(R.id.dialog_btn3).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog3();
                    }
                });
        findViewById(R.id.dialog_btn4).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog4();
                    }
                });
        findViewById(R.id.dialog_btn5).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog5();
                    }
                });
    }

    /**
     * 显示确认对话框
     */
    private void showDialog1() {
     //定义一个AlertDialog.Builder对象 AlertDialog.Builder builder
= new AlertDialog.Builder(this); builder.setTitle("确认对话框");//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标 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(); } });
     //创建、并显示对话框 AlertDialog dialog
= builder.create();//获取dialog dialog.show();//显示对话框 } /** * 显示单选按钮对话框 */ private void showDialog2() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择性别");//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标 builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String str = single_list[which]; Toast.makeText(MainActivity.this, "这个人是"+str+"!", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示对话框 } /** * 显示多选按钮对话框 */ private void showDialog3() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("爱好");//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标 builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ Toast.makeText(MainActivity.this, "我喜欢上了"+multi_list[which]+"!", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "我不喜欢"+multi_list[which]+"了!", Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss();//隐藏对话框 } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示对话框 } /** * 显示列表对话框 */ private void showDialog4() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("部门列表");//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标
     //为对话框设置多个列表
builder.setItems(item_list,
       //为列表项的单击事件设置监听器
      new DialogInterface.OnClickListener() {            
            @Override
        //该方法的which参数代表用户单击了那个列表项
public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "我动了"+item_list[which]+"!", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示对话框 } /** * 显示自定义对话框 */ private void showDialog5() { LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("自定义对话框");//设置标题 builder.setIcon(R.drawable.ic_launcher);//设置图标 builder.setView(view); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示对话框 } }

activity_main.xml

<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:orientation="vertical"
    tools:context=".MainActivity" >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn1"
        android:text="确认对话框"
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn2"
        android:text="单选按钮对话框"
        />
     <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn3"
        android:text="多选按钮对话框"
        />
     <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn4"
        android:text="列表对话框"
        />
     <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:id="@+id/dialog_btn5"
        android:text="自定义对话框"
        />
</LinearLayout>

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="输入内容..."
        android:layout_weight="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginLeft="10dip"
        />
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:src="@drawable/topimg"
        />
</LinearLayout>

 

自定义Dialog:

 

转载于:https://www.cnblogs.com/crazyzx/articles/5382116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值