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

使用对话框 —— Dialog

时间:2015-02-06 14:54:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 

对话框就是一般的弹出窗口,主要用来提示用户,和用户交互。

 

创建Activity对话框

使用Activity模拟对话框。这个比较简单,主要是使用Activity自带的Dialog主题。

 

创建DialogActivity,并在AndroidManifest中注册。

改变DialogActivity的主题:

<activity
            android:theme="@android:style/Theme.Dialog"
            android:name="com.whathecode.usingdialog.DialogActivity"
            android:label="@string/title_activity_dialog" >
</activity>

 

DialogActivity代码示例:

package com.whathecode.usingdialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class DialogActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
    
    
    //用来关闭这个Activity
    public void close(View view)
    {
        finish();
    }
}

 

DialogActivity布局文件:

<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:padding="5dp"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal"
    tools:context=".DialogActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是基于Activity的Dialog" />
    
    <LinearLayout 
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <Button 
            android:id="@+id/confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"
            android:onClick="close"/>
        
        <Button 
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            android:onClick="close"/>
    </LinearLayout>

</LinearLayout>

 

MainActivity代码:

package com.whathecode.usingdialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

public class MainActivity extends FragmentActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void openActivityDialog(View view)
    {
        Intent intent = new Intent(this, DialogActivity.class);
        startActivity(intent);
    }
}

 

运行效果:

技术分享

 

创建单选,多选和带进度条的对话框:

主要是使用AlertDialog类,首先通过创建AlertDialog类的实例,然后使用showDialog显示对话框。

showDialog方法的执行会引发onCreateDialog方法被调用

示例代码:

package com.whathecode.usingdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity
{

    private static final int SINGLE_CHOICE_DIALOG = 0;
    private static final int MULTI_CHOICE_DIALOG = 1;
    private static final int PROGRESS_DIALOG = 2;
    protected static final int MAX_PROGRESS = 30;
    private CharSequence items[] = new String[] { "apple", "google",
            "microsoft" };
    private boolean checkedItems[] = new boolean[3];
    
    
    private Handler progressHandler;
    private int progress;
    protected ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        progressHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (progress >= MAX_PROGRESS) {
                    progressDialog.dismiss(); //关闭progressDialog
                } else {
                    progress++;
                    
                    //进度条加1
                    progressDialog.incrementProgressBy(1);
                    //只要当前进度小于总进度,每个100毫秒发送一次消息
                    progressHandler.sendEmptyMessageDelayed(0, 100);
                }
            }
        };
    }

    public void openActivityDialog(View view)
    {
        Intent intent = new Intent(this, DialogActivity.class);
        startActivity(intent);
    }

    //显示单选对话框
    public void openSinglechoiceDialog(View view)
    {
        showDialog(SINGLE_CHOICE_DIALOG);
    }

    //显示多选对话框
    public void openMultichoiceDialog(View view)
    {
        showDialog(MULTI_CHOICE_DIALOG);
    }
    
    //显示进度条对话框
    public void openProgressDialog(View view)
    {
        showDialog(PROGRESS_DIALOG);
        progress = 0;
        progressDialog.setProgress(0);
        progressHandler.sendEmptyMessage(0);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
        case SINGLE_CHOICE_DIALOG:
            return createSingleChoiceDialog();

        case MULTI_CHOICE_DIALOG:
            return createMultichoiceDialog();
            
        case PROGRESS_DIALOG:
            return createProgressDialog();

        default:
            break;
        }
        return null;
    }
    
    
    /**
     * 创建单选对话框
     * 
     */
    public Dialog createSingleChoiceDialog()
    {
        return new AlertDialog.Builder(this)
                .setTitle("单选对话框")  //设置对话框标题
                .setNegativeButton("取消", null)  //设置取消按钮钮
                .setPositiveButton("确定", null) //设置确定按
                .setSingleChoiceItems(items, 0,    //绑定数据
                        new DialogInterface.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which)
                            {
                                Toast.makeText(getBaseContext(),
                                        items[which].toString(),
                                        Toast.LENGTH_SHORT).show();
                            }
                        }).create();
    }

    /**
     * 创建多选对话框
     * 
     */
    public Dialog createMultichoiceDialog()
    {
        return new AlertDialog.Builder(this)
                .setTitle("多选对话框")  //设置对话框标题
                .setNegativeButton("取消", null)  //设置取消按钮
                .setPositiveButton("确定", null) //设置确定按钮
                .setMultiChoiceItems(items, checkedItems,  //绑定数据
                        new DialogInterface.OnMultiChoiceClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked)
                            {
                                Toast.makeText(
                                        getBaseContext(),
                                        isChecked ? items[which] + " check"
                                                : items[which] + " uncheck",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }).create();
    }
    
    /**
     * 创建带进度条的对话框
     * 
     */
    public Dialog createProgressDialog()
    {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("下载对话框");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_PROGRESS);
        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                
            }
        });
        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener()
        {
            
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                
            }
        });

        return progressDialog;
    }
}

 

运行效果:

技术分享

 

这里比较难理解还是ProgressDialog,因为它需要增加进度。这里我们通过向Activity线程发送消息,

从而能够使用progressDialog.incrementProgressBy(1)方法递增进度条。

使用对话框 —— Dialog

标签:

原文地址:http://www.cnblogs.com/ai-developers/p/4277014.html

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