Android对话框使用详解(二)
- View view = getLayoutInflater().inflate(R.layout.dialog, null); //装载R.layout.popup对应的界面布局
- final PopupWindow popup = new PopupWindow(view, 500, 300); //指定布局资源,窗口高度、长度
- // popup.showAsDropDown(view); //以下拉方式显示
- popup.showAtLocation(findViewById(R.id.dialogBtn), Gravity.CENTER, 100, 100);
- popup.dismiss();
- Calendar calendar = Calendar.getInstance();
- new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
- public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
- Toast.makeText(MainActivity.this, "你选择的日期为:"+year+":"+monthOfYear+":"
- +dayOfMonth, Toast.LENGTH_SHORT).show();
- }
- }
- //初始化日期
- , calendar.get(Calendar.YEAR)
- , calendar.get(Calendar.MONTH)
- , calendar.get(Calendar.DAY_OF_MONTH)).show();
- /************************************************************************/
- Calendar calendar = Calendar.getInstance();
- new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
- public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
- Toast.makeText(MainActivity.this, "你选择的时间为:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
- }
- }
- //初始化时间
- , calendar.get(Calendar.HOUR_OF_DAY)
- , calendar.get(Calendar.MINUTE),
- //true表示采用24小时制
- true).show();
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //进度条
- ProgressDialog progressDialog = null;
- final static int MAX_PROGRESS = 100;
- private int[] data = new int[60];
- int progressStatus = 0;
- int hasData = 0;
- }
- /**
- * 定义一个负责更新进度条的Handler
- * */
- Handler handler = new Handler(){
- public void handleMessage(Message msg) {
- if(msg.what == 0x110){
- progressDialog.setProgress(progressStatus);
- }
- };
- };
- public void dialogBtn(View v){
- /**
- * 创建一个进度条对话框
- * */
- progressDialog = new ProgressDialog(this); //实例化一个ProgressDialog对象
- progressDialog.setTitle("任务进行中");
- progressDialog.setMessage("任务正在执行中,请稍后");
- progressDialog.setCancelable(false); //设置对话框能够触屏关闭
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置对话框的进度条风格
- progressDialog.setIndeterminate(false); //设置对话框的进度条是否显示进度
- progressDialog.show(); //显示对话框
- /**
- * 创建一个子线程,定时发送消息给Handler
- * */
- new Thread(new Runnable(){
- public void run() {
- while(progressStatus < MAX_PROGRESS){
- progressStatus = MAX_PROGRESS*doWork()/data.length; //获取耗时操作的完成百分比
- handler.sendEmptyMessage(0x110);
- }
- if(progressStatus >= MAX_PROGRESS){ //任务完成,退出对话框
- progressDialog.dismiss();
- }
- }
- }).start();
- }
- /**
- * 模拟一个耗时的操作
- * */
- protected int doWork() {
- data[hasData++] = (int)(Math.random()*100);
- try{
- Thread.sleep(500); //0.5s更新一次
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- return hasData;
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。