PopupWindow,DatePickerDialog,TimePickerDialog,ProgressDialog的使用
来源:互联网 发布:虾米音乐vip 知乎 编辑:程序博客网 时间:2024/05/21 11:26
- 对话框风格的窗口实现
- PopupWindow的使用
- mainxml代码
- popupxml代码
- Activity代码
- 使用DatePickerDialogTimePickerDialog
- 使用步骤
- 代码区
- 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 0
- PopupWindow,DatePickerDialog,TimePickerDialog,ProgressDialog的使用
- DatePickerDialog、TimePickerDialog的使用
- DatePickerDialog、TimePickerDialog、PopupWindow、Notification
- 对话框之AlertDialog、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择)、ProgressDialog(进度)、PopupWindow
- android学习--DatePickerDialog和TimePickerDialog的使用
- DatePicker,TimePicker,DatePickerDialog以及TimePickerDialog的使用
- DatePickerDialog和TimePickerDialog的简单使用
- DatepickerDialog与TimepickerDialog使用
- Android AlertDialog,PopupWindow,DatePickerDialog,ProgressDialog的详细介绍
- 自定义Dialog;Notification;PopupWindow;DatePickerDialog;TimePickerDialog
- 自定义Dialog,DatePickerDialog,TimePickerDialog,PopupWindow,Notification
- DatePickerDialog和TimePickerDialog使用示例
- Fragment中使用DatePickerDialog、TimePickerDialog
- Android_使用DatePickerDialog和TimePickerDialog
- 组件的使用(四)DatePickerDialog和TimePickerDialog的使用
- 自定义的时钟,DatePickerDialog 和 TimePickerDialog的使用
- Android中关于DatePickerDialog与TimePickerDialog的结合使用
- Android中DatePickerDialog对话框和TimePickerDialog对话框的使用
- C++ 解析flv文件
- C10K问题
- jQuery is not defined
- EOJ 1802 铁路调度
- 普通服务器与负载均衡服务器下的$_SERVER变量对比
- PopupWindow,DatePickerDialog,TimePickerDialog,ProgressDialog的使用
- Android 获取验证码使用倒计时
- 链表的引入和简单应用
- OSX和Linux设置环境变量的方法
- vim程序编辑器
- 目标检测光流法(一):对光流法的认识
- SQL高级查询—实验报告
- 【LeetCode-209】Minimum Size Subarray Sum(C++)
- react 数据流学习笔记