1. 程式人生 > 實用技巧 >吳裕雄--天生自然ANDROID開發學習:2.6.0 其他幾種常用對話方塊基本使用

吳裕雄--天生自然ANDROID開發學習:2.6.0 其他幾種常用對話方塊基本使用

1.ProgressDialog(進度條對話方塊)的基本使用
我們建立進度條對話方塊的方式有兩種:

1.直接呼叫ProgressDialog提供的靜態方法show()顯示
2.建立ProgressDialog,再設定對話方塊的引數,最後show()出來

關鍵實現程式碼:

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;
    }
}
2.DatePickerDialog(日期選擇對話方塊)與TimePickerDialog(時間選擇對話方塊)
DatePickerDialog(上下文;DatePickerDialog.OnDateSetListener()監聽器;年;月;日)
TimePickerDialog(上下文;TimePickerDialog.OnTimeSetListener()監聽器;小時,分鐘,是否採用24小時制)

關鍵實現程式碼:

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;
        }
    }
}