国信安教育
我不需要颜值有多高,只想将我的培训经历淡淡地放入到我的博客里面,不管是技术,还是生活。可能我的技术不高大尚,可能我的生活很平淡。但是我只想做回真正的自己。

要想应用显示一个对话框,我们借助AlertDialog.Builder组件来完成对话框的创建

  • 创建对话框标题图片
  • 创建对话框标题文字
  • 创建对话框确定和取消 创建复选框

完成这个效果我们分步来做

  • 首先在Activity中重写protected Dialog onCreateDialog(int id, Bundle args) {...}
  • 然后通过调用父类的showDialog(0);方法来执行onCreateDialog
  • 创建对话框需要使用AlertDialog.Builder,它是对话框生成器AlertDialog.Builder

AlertDialog.Builder对象常用的方法

  • setIcon(...): 设置对话框的图片
  • setTitle(...): 设置对话框的标题
  • setPositiveButton(...): 设置对话框的确认按钮,以及点击确认按钮的事件监听程序
  • setNegativeButton(...): 设置对话框的取消按钮,以及点击取消按钮的事件监听程序
  • setMultiChoiceItems(...): 设置对话框的中多选框效果,以及选中多选框元素会触发事件监听程序

布局文件代码

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_dialog"
        android:onClick="onClick">
        
    </Button>

</RelativeLayout>

注意<Button.../>标签中的android:onClick="onClick"定义了界面按钮的触发事件,相当于点击按钮就会执行程序代码

整个MainActivity代码如下

package com.gxa;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    CharSequence[] items = {"Google", "Apple", "Mircosoft"};
    boolean[] itemChecked = new boolean[items.length];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @SuppressWarnings("deprecation")
    public void onClick(View view) {
        showDialog(0);
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch(id) {
        case 0:
            AlertDialog.Builder builder = new AlertDialog.Builder(this); //对话框生成器AlertDialog.Builder
            builder.setIcon(R.drawable.ic_launcher); //设置对话框图片
            builder.setTitle("对话框"); //设置对话框标题
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置对话框确定按钮
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "确定", Toast.LENGTH_SHORT).show();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置对话框取消按钮
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "取消", Toast.LENGTH_SHORT).show();
                }
            });
            builder.setMultiChoiceItems(items, itemChecked, new DialogInterface.OnMultiChoiceClickListener() { //设置对话框复选按钮
                
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(getBaseContext(), items[which] + (isChecked ? " checked": " unchecked"), Toast.LENGTH_SHORT).show();
                }
            });
            return builder.create();
        }
        return null;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

文章内容来自:国信安刘阳

 

posted on 2016-01-26 15:05  国信安教育  阅读(142)  评论(0编辑  收藏  举报