Andorid里的AlertDialog对话框的一个demo

下午做了1个多小时的公交车才到家,说实话有点疲倦,但是又不能睡这么的早,内心自责,于是来写一个小弹框。

以前做web开发的时候,弹框什么的必不可少,那APP应用里也是如此。

首先来介绍下Android时的AlertDialog。

AlertDialog是Dialog的一个子类 ,写一个demo 让它弹出时间日期,在Android里有一个日期组件 ,DatePicker。

 

来个Button 来激活这个组件。

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

 

UI主线程代码:

 

package com.example.rf;

import com.example.rf.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;

public class DialogActivity extends Activity {

    private Button mButton;
    
    
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.dialog);
        
        
        mButton = (Button) findViewById(R.id.button1);
        
        mButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog().show();
                
            }
        });
        
        
    }
    
    
    /**
     * 使用AlertDialog.Bundler类,以流的接口的方式创建一个AlertDialog.Bundle实例,构造函数参数 是this 当前activity
     * 调用三个方法,setTitle(); 给这个弹框加一个Title
     * setView(View)  这个意思就是把我们实例化的那个日期组件放到这个弹框的中间, 做为主视图、 我们的是DatePicker动态创建的,
     * 当然这里也可以自己写一个xml 用getActivity.getLayoutInflater.inflater(...)来动态加载也可以。
     * Android有3种可用于对话框的按钮,position, native, negative 这三种有什么不同,请自行百度。这里用的是Position
     * setPositiveButton(arg1, arg2)
     * 一个是字符串资源, 一个是实现AlertDialog.Interface.OnclickListence接口的对象, 这个我们不做讨论,写入null
     * @return  Dialog
     */
    
    private Dialog alertDialog() {
        DatePicker dp = new DatePicker(this);
        return new AlertDialog.Builder(this).setView(dp).setTitle("haha").setPositiveButton(android.R.string.ok, null).create();
    }
    
}

 

posted @ 2015-06-01 22:18  savey  阅读(145)  评论(0)    收藏  举报
savey:better life,do it best