PopupWindow,DatePickerDialog,TimePickerDialog,ProgressDialog的使用

对话框风格的窗口
PopupWindow
DateDialog
ProgressDialog

对话框风格的窗口实现

在Manifest.xml中指定该窗口以对话框风格显示!简单的使用Activity来显示界面布局!

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Material.Dialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

PopupWindow的使用:

创建步骤:
    1:调用PopupWindow的构造器创建PopupWindow对象
    2:调用PopupWindow的showAsDropDown()将PopupWindow作为v组件的下拉组件显示处出来,或调用PopupWindow的showAtLocation()将PopupWindow在指定的位置显示出来

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
<Button android:id="@+id/bn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="弹出Popup窗口" 
    />
</LinearLayout>

popup.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
<ImageView 
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="85dp"
    android:src="@drawable/java"
    />
<Button
    android:id="@+id/close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="关闭" 
    />
</LinearLayout>

Activity代码:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 装载R.layout.popup对应的界面布局
        View root = this.getLayoutInflater().inflate(R.layout.popup, null);
        // 创建PopupWindow对象
        final PopupWindow popup = new PopupWindow(root, 560, 720);
        Button button = (Button) findViewById(R.id.bn);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // 以下拉方式显示
                // popup.showAsDropDown(v);
                //将PopupWindow显示在指定位置
                popup.showAtLocation(findViewById(R.id.bn),
                    Gravity.CENTER, 20,20);
            }
        });
        // 获取PopupWindow中的“关闭”按钮
        root.findViewById(R.id.close).setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    // 关闭PopupWindow
                    popup.dismiss(); // ①
                }
            });
    }
}

使用DatePickerDialog,TimePickerDialog

使用步骤;

1:通过new关键字创建DatePickerDialog,TimePickerDialog实例,调用它的show()方法显示出来
2:为其绑定监听器。

代码区

Acitivity代码:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button dateBn = (Button)findViewById(R.id.dateBn);
        Button timeBn = (Button)findViewById(R.id.timeBn);
        //为“设置日期”按钮绑定监听器
        dateBn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                Calendar c = Calendar.getInstance();
                // 直接创建一个DatePickerDialog对话框实例,并将它显示出来
                new DatePickerDialog(MainActivity.this,
                    // 绑定监听器
                    new DatePickerDialog.OnDateSetListener()
                    {
                        @Override
                        public void onDateSet(DatePicker dp, int year,
                            int month, int dayOfMonth)
                        {
                            EditText show = (EditText) findViewById(R.id.show);
                            show.setText("您选择了:" + year + "年" + (month + 1)
                                + "月" + dayOfMonth + "日");
                        }
                    }
                    //设置初始日期
                    , c.get(Calendar.YEAR)
                    , c.get(Calendar.MONTH)
                    , c.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        //为“设置时间”按钮绑定监听器
        timeBn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                Calendar c = Calendar.getInstance();
                // 创建一个TimePickerDialog实例,并把它显示出来
                new TimePickerDialog(MainActivity.this,
                    // 绑定监听器
                    new TimePickerDialog.OnTimeSetListener()
                    {
                        @Override
                        public void onTimeSet(TimePicker tp, int hourOfDay,
                            int minute)
                        {
                            EditText show = (EditText) findViewById(R.id.show);
                            show.setText("您选择了:" + hourOfDay + "时"
                                + minute + "分");
                        }
                    }
                    //设置初始时间
                    , c.get(Calendar.HOUR_OF_DAY)
                    , c.get(Calendar.MINUTE)
                    //true表示采用24小时制
                    , true).show();
            }
        });
    }
}

ProgressDialog的使用方法:

1:创建Progress的实例,调用show();
2:创建ProgressDialog实例,然后调用方法对对话框进度条进行设置,设置完成后使其显示

代码区:

Activity代码:

public class MainActivity extends Activity
{
    final static int MAX_PROGRESS = 100;
    // 该程序模拟填充长度为100的数组
    private int[] data = new int[50];
    // 记录进度对话框的完成百分比
    int progressStatus = 0;
    int hasData = 0;
    ProgressDialog pd1,pd2;
    // 定义一个负责更新的进度的Handler
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            // 表明消息是由该程序发送的
            if (msg.what == 0x123)
            {
                pd2.setProgress(progressStatus);
            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void showSpinner(View source)
    {
        // 调用静态方法显示环形进度条
        ProgressDialog.show(this, "任务执行中"
                , "任务执行中,请等待", false, true); // ①
    }
    public void showIndeterminate(View source)
    {
        pd1 = new ProgressDialog(MainActivity.this);
        // 设置对话框的标题
        pd1.setTitle("任务正在执行中");
        // 设置对话框显示的内容
        pd1.setMessage("任务正在执行中,敬请等待...");
        // 设置对话框能用“取消”按钮关闭
        pd1.setCancelable(true);
        // 设置对话框的进度条风格
        pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 设置对话框的进度条是否显示进度
        pd1.setIndeterminate(true);
        pd1.show(); // ②
    }
    public void showProgress(View source)
    {
        // 将进度条的完成进度重设为0
        progressStatus = 0;
        // 重新开始填充数组
        hasData = 0;
        pd2 = new ProgressDialog(MainActivity.this);
        pd2.setMax(MAX_PROGRESS);
        // 设置对话框的标题
        pd2.setTitle("任务完成百分比");
        // 设置对话框显示的内容
        pd2.setMessage("耗时任务的完成百分比");
        // 设置对话框不能用“取消”按钮关闭
        pd2.setCancelable(false);
        // 设置对话框的进度条风格
        pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 设置对话框的进度条是否显示进度
        pd2.setIndeterminate(false);
        pd2.show(); // ③
        new Thread()
        {
            public void run()
            {
                while (progressStatus < MAX_PROGRESS)
                {
                    // 获取耗时操作的完成百分比
                    progressStatus = MAX_PROGRESS
                        * doWork() / data.length;
                    // 发送空消息到Handler
                    handler.sendEmptyMessage(0x123);
                }
                // 如果任务已经完成
                if (progressStatus >= MAX_PROGRESS)
                {
                    // 关闭对话框
                    pd2.dismiss();
                }
            }
        }.start();
    }
    // 模拟一个耗时的操作
    public int doWork()
    {
        // 为数组元素赋值
        data[hasData++] = (int) (Math.random() * 100);
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return hasData;
    }
}

如果大家有什么问题,请留言。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
PopupWindowAndroid 系统中的一个弹出窗口,可以显示在当前界面之上。使用 PopupWindow 可以实现下拉菜单、提示框、对话框等功能。 使用 PopupWindow 的步骤如下: 1.创建 PopupWindow 对象 ```java PopupWindow popupWindow = new PopupWindow(context); ``` 2.设置 PopupWindow布局和宽高 ```java View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); ``` 3.设置 PopupWindow 的背景和动画效果 ```java popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); ``` 4.设置 PopupWindow 的位置和显示方式 ```java popupWindow.showAsDropDown(anchorView, xoff, yoff, gravity); ``` 其中,`anchorView` 是弹出窗口的锚点 View,`xoff` 和 `yoff` 是弹出窗口相对于锚点 View 的偏移量,`gravity` 是弹出窗口的位置,可以是 `Gravity.LEFT`、`Gravity.RIGHT`、`Gravity.TOP` 或 `Gravity.BOTTOM`。 5.处理 PopupWindow 的事件 ```java popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // 处理弹出窗口消失事件 } }); ``` 以上就是 PopupWindow 的基本使用方法。需要注意的是,PopupWindow 的宽高应该根据实际需要设置,以避免出现显示不全或者过大的情况。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值