<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 2.6.0 其他幾種常用對話框基本使用 ## 本節引言: > 上節我們對Dialog的父類:AlertDialog進行了學習,而本節我們來學習下幾個常用的 Dialog的基本使用,他們分別是:ProgressDialog(進度條對話框),DatePickerDialog (日期選擇對話框)和TimePickerDialog(時間選擇對話框)~,話不多說,開始本節內容~ ## 1.ProgressDialog(進度條對話框)的基本使用 > 我們創建進度條對話框的方式有兩種: > > * **1**.直接調用ProgressDialog提供的靜態方法show()顯示 > * **2**.創建ProgressDialog,再設置對話框的參數,最后show()出來 **代碼示例**: **運行效果圖**: ![](http://www.runoob.com/wp-content/uploads/2015/10/1735732.jpg) **關鍵實現代碼**: **MainActivity.java**: ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_one; private Button btn_two; private Button btn_three; private ProgressDialog pd1 = null; private ProgressDialog pd2 = null; private final static int MAXVALUE = 100; private int progressStart = 0; private int add = 0; private Context mContext = null; //定義一個用于更新進度的Handler,因為只能由主線程更新界面,所以要用Handler傳遞信息 final Handler hand = new Handler() { @Override public void handleMessage(Message msg) { //這里的話如果接受到信息碼是123 if(msg.what == 123) { //設置進度條的當前值 pd2.setProgress(progressStart); } //如果當前大于或等于進度條的最大值,調用dismiss()方法關閉對話框 if(progressStart >= MAXVALUE) { pd2.dismiss(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); } private void bindViews() { btn_one = (Button) findViewById(R.id.btn_one); btn_two = (Button) findViewById(R.id.btn_two); btn_three = (Button) findViewById(R.id.btn_three); btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_one: //這里的話參數依次為,上下文,標題,內容,是否顯示進度,是否可以用取消按鈕關閉 ProgressDialog.show(MainActivity.this, "資源加載中", "資源加載中,請稍后...",false,true); break; case R.id.btn_two: pd1 = new ProgressDialog(mContext); //依次設置標題,內容,是否用取消按鈕關閉,是否顯示進度 pd1.setTitle("軟件更新中"); pd1.setMessage("軟件正在更新中,請稍后..."); pd1.setCancelable(true); //這里是設置進度條的風格,HORIZONTAL是水平進度條,SPINNER是圓形進度條 pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd1.setIndeterminate(true); //調用show()方法將ProgressDialog顯示出來 pd1.show(); break; case R.id.btn_three: //初始化屬性 progressStart = 0; add = 0; //依次設置一些屬性 pd2 = new ProgressDialog(MainActivity.this); pd2.setMax(MAXVALUE); pd2.setTitle("文件讀取中"); pd2.setMessage("文件加載中,請稍后..."); //這里設置為不可以通過按取消按鈕關閉進度條 pd2.setCancelable(false); pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //這里設置的是是否顯示進度,設為false才是顯示的哦! pd2.setIndeterminate(false); pd2.show(); //這里的話新建一個線程,重寫run()方法, new Thread() { public void run() { while(progressStart < MAXVALUE) { //這里的算法是決定進度條變化的,可以按需要寫 progressStart = 2 * usetime() ; //把信息碼發送給handle讓更新界面 hand.sendEmptyMessage(123); } } }.start(); break; } } //這里設置一個耗時的方法: private int usetime() { add++; try{ Thread.sleep(100); }catch (InterruptedException e) { e.printStackTrace(); } return add; } } ``` 代碼比較簡單,而關于Progress的東西我們已經在前面學習過了,這里就不啰嗦了~ ## 2.DatePickerDialog(日期選擇對話框)與TimePickerDialog(時間選擇對話框) > 先要說明一點: Date/TimePickerDialog只是供用戶來選擇日期時間,對于android系統的系統時間, 日期沒有任何影響,google暫時沒有公布系統日期時間設置的API, 如果要在app中設置的話,要重新編譯android的系統源碼,非常麻煩! > > 他們兩個的構造方法非常相似: **DatePickerDialog**(上下文;DatePickerDialog.OnDateSetListener()監聽器;年;月;日) > **TimePickerDialog**(上下文;TimePickerDialog.OnTimeSetListener()監聽器;小時,分鐘,是否采用24小時制) **代碼示例**: **運行效果圖**: ![](http://www.runoob.com/wp-content/uploads/2015/10/47542561.jpg) **關鍵實現代碼**: **MainActivity.java**: ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_date; private Button btn_time; private String result = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); } private void bindViews() { btn_date = (Button) findViewById(R.id.btn_date); btn_time = (Button) findViewById(R.id.btn_time); btn_date.setOnClickListener(this); btn_time.setOnClickListener(this); } @Override public void onClick(View v) { result = ""; switch (v.getId()){ case R.id.btn_date: Calendar cale1 = Calendar.getInstance(); new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //這里獲取到的月份需要加上1哦~ result += "你選擇的是"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show(); } } ,cale1.get(Calendar.YEAR) ,cale1.get(Calendar.MONTH) ,cale1.get(Calendar.DAY_OF_MONTH)).show(); break; case R.id.btn_time: Calendar cale2 = Calendar.getInstance(); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { result = ""; result += "您選擇的時間是:"+hourOfDay+"時"+minute+"分"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show(); } }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show(); break; } } } ``` 代碼同樣很簡單,就不解釋了~ ## 3.代碼下載: [DialogDemo.zip](http://www.runoob.com/wp-content/uploads/2015/10/DialogDemo.zip) [DialogDemo1.zip](http://www.runoob.com/wp-content/uploads/2015/10/DialogDemo1.zip) ## 本節小結: > 好的,本節介紹了三個常用的Dialog,相比起以前的4.x的版本,5.0的這些原生控件, 顯然要好看得多~就說這么多,謝謝~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看