網站首頁 編程語言 正文
本文實例為大家分享了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(); ? ? } }
原文鏈接:https://blog.csdn.net/qq_41218412/article/details/89287353
相關推薦
- 2022-06-18 Elasticsearches通過坐標位置實現對附近人的搜索_其它綜合
- 2022-10-20 C#?Random類隨機函數實例詳解_C#教程
- 2022-07-13 Atom 常用快捷鍵
- 2023-04-19 Git 推送報錯:error: GH007: Your push would publish a p
- 2022-05-06 使用Docker部署打包發布springboot項目_docker
- 2021-12-18 linux下安裝redis圖文詳細步驟_Redis
- 2022-04-25 JQuery異步post上傳表單數據標準化模板_jquery
- 2023-08-28 Antd的日期選擇器中文化配置
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支