欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?studio實現(xiàn)日期?、時間選擇器與進度條

 更新時間:2022年01月20日 08:14:58   作者:Be?your?bubble  
這篇文章主要為大家詳細介紹了Android?studio實現(xiàn)日期、時間選擇器與進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android studio實現(xiàn)日期 、時間選擇器與進度條,供大家參考,具體內(nèi)容如下

日期選擇器

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();
? ? }

注意:此按鈕響應(yīng)需要在按鈕布局文件里面加一句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="跳轉(zhuǎn)"
? ? ? ? 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( "任務(wù)進行中" );
? ? ? ? pd.setMessage( "請稍后..." );
? ? ? ? pd.setCancelable( true );
? ? ? ? pd.setProgressStyle( ProgressDialog.STYLE_SPINNER );
? ? ? ? pd.show();
? ? }
? ? public void onclick(View v){//按鈕的一種方法
? ? ? ? showprogress();
? ? }
}

做一個小練習(xí)來模擬一下(可用在刷新列表啥的)

.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( "任務(wù)進行中" );
? ? ? ? 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();//發(fā)送完關(guān)閉
? ? ? ? }
? ? };
? ? 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 );//向主干道發(fā)送消息,子線程
? ? ? ? ? ? }
? ? ? ? }.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( "任務(wù)進行中" );
? ? ? ? pd.setMessage( "請稍后..." );
? ? ? ? pd.setCancelable( true );
? ? ? ? pd.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL);//風(fēng)格
? ? ? ? pd.setMax(100);//下載數(shù)量啥的
? ? ? ? pd.show();
? ? }
? ? Handler handler=new Handler( ?){//接收
? ? ? ? @Override
? ? ? ? public void handleMessage(Message msg) {//在主線程(UI)
? ? ? ? ? ? if(msg.what==0)//接受的信息判斷,0結(jié)束
? ? ? ? ? ? ? ? pd.dismiss();//發(fā)送完關(guān)閉
? ? ? ? ? ? else if( msg.what==1){
? ? ? ? ? ? ? ?pd.setProgress( msg.arg1 );//接受的信息判斷如果是1,說明進度沒結(jié)束,加一
? ? ? ? ? ? }
? ? ? ? }
? ? };

? ? 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;//中間發(fā)送消息都是一,直到0結(jié)束,所以不結(jié)束
? ? ? ? ? ? ? ? ? ? handler.sendMessage( mag );//增長的信息每次發(fā)送一次
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? handler.sendEmptyMessage( 0 );//向主干道發(fā)送消息,子線程
? ? ? ? ? ? }
? ? ? ? }.start();
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論