Android studio实现日期 、时间选择器与进度条

Emily ·
更新时间:2024-09-20
· 1001 次阅读

本文实例为大家分享了Android studio实现日期 、时间选择器与进度条,供大家参考,具体内容如下

日期选择器 public void onclick(View v){         Calendar calendar=Calendar.getInstance();         new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() {             @Override             public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {                 String text = "你选择了:" + year + "年" + (month + 1) + "月" + dayOfMonth + "日";                 Toast.makeText( MainActivity.this, text, Toast.LENGTH_SHORT ).show();             }         }         ,calendar.get(Calendar.YEAR)         ,calendar.get(Calendar.MONTH)         ,calendar.get(Calendar.DAY_OF_MONTH)).show();     }

注意:此按钮响应需要在按钮布局文件里面加一句android:onClick="onclick"

时间选择器

ProgressDialog一般用于表示当前操作比较耗时间,让用户耐心等待

 public void onclick(View v){         Calendar calendar=Calendar.getInstance();         new TimePickerDialog( this, new TimePickerDialog.OnTimeSetListener() {             @Override             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {                 String text="你选择了"+hourOfDay+"时"+minute+"分";                 Toast.makeText( MainActivity.this, text, Toast.LENGTH_SHORT ).show();             }         }         ,calendar.get(Calendar.HOUR_OF_DAY)         ,calendar.get(Calendar.MINUTE),true).show();     } 进度条

1、圆圈

.xml:

<?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/but"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="跳转"         android:onClick="onclick"/> </LinearLayout>

.java:

package com.example.catalogin;         import android.app.ProgressDialog;         import android.support.v7.app.AppCompatActivity;         import android.os.Bundle;         import android.view.View; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     ProgressDialog pd;     public void showprogress(){         pd=new ProgressDialog(this);         pd.setTitle( "任务进行中" );         pd.setMessage( "请稍后..." );         pd.setCancelable( true );         pd.setProgressStyle( ProgressDialog.STYLE_SPINNER );         pd.show();     }     public void onclick(View v){//按钮的一种方法         showprogress();     } }

做一个小练习来模拟一下(可用在刷新列表啥的)

.java代码改为:

package com.example.catalogin;         import android.app.ProgressDialog;         import android.os.Handler;         import android.os.Message;         import android.support.v7.app.AppCompatActivity;         import android.os.Bundle;         import android.view.View; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     ProgressDialog pd;     public void showprogress(){         pd=new ProgressDialog(this);         pd.setTitle( "任务进行中" );         pd.setMessage( "请稍后..." );         pd.setCancelable( true );         pd.setProgressStyle( ProgressDialog.STYLE_SPINNER );         pd.show();     }     Handler handler=new Handler(  ){         @Override         public void handleMessage(Message msg) {//在主线程(UI)             pd.dismiss();//发送完关闭         }     };     public void onclick(View v){         showprogress();         //新建一个子线程         new Thread(){//new Thread 说明并行进行,在小路跑             public void run(){                 for(int i=0;i<=3;i++){                     try{                         Thread.sleep( 1000 );                     } catch (InterruptedException e) {                         e.printStackTrace();                     }                 }                 handler.sendEmptyMessage( 0 );//向主干道发送消息,子线程             }         }.start();     } }

效果为自己跑完三秒之后就自动消失

2、水平

.java 文件代码改为:

package com.example.catalogin;         import android.app.ProgressDialog;         import android.os.Handler;         import android.os.Message;         import android.support.v7.app.AppCompatActivity;         import android.os.Bundle;         import android.view.View; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     ProgressDialog pd;     public void showprogress(){         pd=new ProgressDialog(this);         pd.setTitle( "任务进行中" );         pd.setMessage( "请稍后..." );         pd.setCancelable( true );         pd.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL);//风格         pd.setMax(100);//下载数量啥的         pd.show();     }     Handler handler=new Handler(  ){//接收         @Override         public void handleMessage(Message msg) {//在主线程(UI)             if(msg.what==0)//接受的信息判断,0结束                 pd.dismiss();//发送完关闭             else if( msg.what==1){                pd.setProgress( msg.arg1 );//接受的信息判断如果是1,说明进度没结束,加一             }         }     };     public void onclick(View v){         showprogress();         //新建一个子线程         new Thread(){//new Thread 说明并行进行,在小路跑             public void run(){                 for(int i=0;i<=100;i++){                     try{                         Thread.sleep( 100 );                     } catch (InterruptedException e) {                         e.printStackTrace();                     }                     Message mag=Message.obtain();                     mag.arg1=i;//增长的进度丢进去                     mag.what=1;//中间发送消息都是一,直到0结束,所以不结束                     handler.sendMessage( mag );//增长的信息每次发送一次                 }                 handler.sendEmptyMessage( 0 );//向主干道发送消息,子线程             }         }.start();     } }



选择器 studio 选择 进度条 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章