码迷,mamicode.com
首页 > 其他好文 > 详细

对话框

时间:2015-11-20 12:01:59      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

设置了3个对话框,第一个是基本对话框

第二个是单选对话框      setSingleChoiceItems

第三个是多选对话框        setMultiChoiceItems

如下是代码

package org.xml.demo.viewpager;

import ogg.huanxin.huadong.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class DialogText extends Activity implements OnClickListener {
    private Button button;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(R.layout.dialogtext);
        // 取得控件
        button = (Button) findViewById(R.id.bb_dialog_1);
        button2 = (Button) findViewById(R.id.bb_dialog_2);
        button3 = (Button) findViewById(R.id.bb_dialog_3);

        // 设置控件的监听事件
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        /*
         * 显示一个对话框
         */
        case R.id.bb_dialog_1:
            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 arg0, int arg1) {
                            // TODO Auto-generated method stub
                            Toast.makeText(DialogText.this, "你点击了删除",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            builder.setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub
                        }
                    });
            // 创建并显示对话框
            builder.create().show();

            break;
        /*
         * 显示一个单选对话框
         */
        case R.id.bb_dialog_2:
            AlertDialog.Builder builder2 = new Builder(this);
            builder2.setTitle("请选择性别");
            final String[] items = new String[] { "男", "女" };
            // checkedItem:指定哪个项目被选中,默认为0表示选中第一个项目,-1表示没有项目被选中
            builder2.setSingleChoiceItems(items, -1,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            Toast.makeText(DialogText.this,
                                    "你选择的性别是 ---" + items[which],
                                    Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    });
            builder2.create().show();
            break;

case R.id.bb_dialog_3: AlertDialog.Builder builder3 = new Builder(this); builder3.setTitle("选择你觉得帅的人"); final String[] itemss = new String[] { "三藏", "悟空", "八戒", "悟净" }; // 默认选中,这里设置前2个为默认选中 final boolean[] checkedItems = new boolean[] { true, true, false, false }; /* * Builder * android.app.AlertDialog.Builder.setMultiChoiceItems(CharSequence * [] items, boolean[] checkedItems, OnMultiChoiceClickListener * listener) */ builder3.setMultiChoiceItems(itemss, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { // which:用户点击的条目的下标 // isChecked:用户是选中该条目还是取消该条目 @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub checkedItems[which] = isChecked; } }); // 设置一个确定按钮 builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String text = ""; for (int i = 0; i < 4; i++) { text += checkedItems[i] ? itemss[i] + "," : ""; } Toast.makeText(DialogText.this, "你觉得帅的人有 " + text, Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); builder3.create().show(); break; default: break; } } }

 

对话框

标签:

原文地址:http://www.cnblogs.com/wangfengdange/p/4979935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!