Android 其他幾種常用對(duì)話框基本使用

2023-03-31 13:43 更新

本節(jié)引言:

上節(jié)我們對(duì)Dialog的父類:AlertDialog進(jìn)行了學(xué)習(xí),而本節(jié)我們來學(xué)習(xí)下幾個(gè)常用的 Dialog的基本使用,他們分別是:ProgressDialog(進(jìn)度條對(duì)話框),DatePickerDialog (日期選擇對(duì)話框)和TimePickerDialog(時(shí)間選擇對(duì)話框)~,話不多說,開始本節(jié)內(nèi)容~


1.ProgressDialog(進(jìn)度條對(duì)話框)的基本使用

我們創(chuàng)建進(jìn)度條對(duì)話框的方式有兩種:

  • 1.直接調(diào)用ProgressDialog提供的靜態(tài)方法show()顯示
  • 2.創(chuàng)建ProgressDialog,再設(shè)置對(duì)話框的參數(shù),最后show()出來

代碼示例

運(yùn)行效果圖

關(guān)鍵實(shí)現(xiàn)代碼

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;

    //定義一個(gè)用于更新進(jìn)度的Handler,因?yàn)橹荒苡芍骶€程更新界面,所以要用Handler傳遞信息
    final Handler hand = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            //這里的話如果接受到信息碼是123
            if(msg.what == 123)
            {
                //設(shè)置進(jìn)度條的當(dāng)前值
                pd2.setProgress(progressStart);
            }
            //如果當(dāng)前大于或等于進(jìn)度條的最大值,調(diào)用dismiss()方法關(guān)閉對(duì)話框
            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:
                //這里的話參數(shù)依次為,上下文,標(biāo)題,內(nèi)容,是否顯示進(jìn)度,是否可以用取消按鈕關(guān)閉
                ProgressDialog.show(MainActivity.this, "資源加載中", "資源加載中,請(qǐng)稍后...",false,true);
                break;
            case R.id.btn_two:
                pd1 = new ProgressDialog(mContext);
                //依次設(shè)置標(biāo)題,內(nèi)容,是否用取消按鈕關(guān)閉,是否顯示進(jìn)度
                pd1.setTitle("軟件更新中");
                pd1.setMessage("軟件正在更新中,請(qǐng)稍后...");
                pd1.setCancelable(true);
                //這里是設(shè)置進(jìn)度條的風(fēng)格,HORIZONTAL是水平進(jìn)度條,SPINNER是圓形進(jìn)度條
                pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd1.setIndeterminate(true);
                //調(diào)用show()方法將ProgressDialog顯示出來
                pd1.show();
                break;
            case R.id.btn_three:
                //初始化屬性
                progressStart = 0;
                add = 0;
                //依次設(shè)置一些屬性
                pd2 = new ProgressDialog(MainActivity.this);
                pd2.setMax(MAXVALUE);
                pd2.setTitle("文件讀取中");
                pd2.setMessage("文件加載中,請(qǐng)稍后...");
                //這里設(shè)置為不可以通過按取消按鈕關(guān)閉進(jìn)度條
                pd2.setCancelable(false);
                pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //這里設(shè)置的是是否顯示進(jìn)度,設(shè)為false才是顯示的哦!
                pd2.setIndeterminate(false);
                pd2.show();
                //這里的話新建一個(gè)線程,重寫run()方法,
                new Thread()
                {
                    public void run()
                    {
                        while(progressStart < MAXVALUE)
                        {
                            //這里的算法是決定進(jìn)度條變化的,可以按需要寫
                            progressStart = 2 * usetime() ;
                            //把信息碼發(fā)送給handle讓更新界面
                            hand.sendEmptyMessage(123);
                        }
                    }
                }.start();
                break;
        }
    }

    //這里設(shè)置一個(gè)耗時(shí)的方法:
    private int usetime() {
        add++;
        try{
            Thread.sleep(100);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return add;
    }
}

代碼比較簡(jiǎn)單,而關(guān)于Progress的東西我們已經(jīng)在前面學(xué)習(xí)過了,這里就不啰嗦了~


2.DatePickerDialog(日期選擇對(duì)話框)與TimePickerDialog(時(shí)間選擇對(duì)話框)

先要說明一點(diǎn): Date/TimePickerDialog只是供用戶來選擇日期時(shí)間,對(duì)于android系統(tǒng)的系統(tǒng)時(shí)間, 日期沒有任何影響,google暫時(shí)沒有公布系統(tǒng)日期時(shí)間設(shè)置的API, 如果要在app中設(shè)置的話,要重新編譯android的系統(tǒng)源碼,非常麻煩!

他們兩個(gè)的構(gòu)造方法非常相似: DatePickerDialog(上下文;DatePickerDialog.OnDateSetListener()監(jiān)聽器;年;月;日)
TimePickerDialog(上下文;TimePickerDialog.OnTimeSetListener()監(jiān)聽器;小時(shí),分鐘,是否采用24小時(shí)制)

代碼示例

運(yùn)行效果圖

關(guān)鍵實(shí)現(xiàn)代碼

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 += "您選擇的時(shí)間是:"+hourOfDay+"時(shí)"+minute+"分";
                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                    }
                }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show();
                break;
        }
    }
}

代碼同樣很簡(jiǎn)單,就不解釋了~


3.代碼下載:

DialogDemo.zip

DialogDemo1.zip


本節(jié)小結(jié):

好的,本節(jié)介紹了三個(gè)常用的Dialog,相比起以前的4.x的版本,5.0的這些原生控件, 顯然要好看得多~就說這么多,謝謝~


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)