天天看點

Dialog對話框之ProgressDialog(進度條),DatePickerDialog,Toast

ProgressDialog(進度條對話框)

擁有ProgressBar 的屬性

ProgressDialog progressDialog = new ProgressDialog();

progressDialog .setIndeterminate(true);//設定不确定性,為true 是不确定性, false為确定

progressDialog .setProgressDrawable(..); //自定義進度條樣式

progressDialog .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定水準.  水準的線或者圈

progressDialog .show(); //彈出對話框

<span style="font-size:14px;">final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("refresh.....");
		dialog.setMessage("進度");
		dialog.setIcon(R.drawable.ic_launcher);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setProgressDrawable(getResources().getDrawable(
				R.drawable.line_hori_progressbar_layer_list));
		dialog.setIndeterminate(false);

		dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "confirm",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(DialogActivity.this, "" + which,
								Toast.LENGTH_SHORT).show();
					}
				});
		dialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "cancel",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(DialogActivity.this, "" + which,
								Toast.LENGTH_SHORT).show();
					}
				});

		new Thread(new Runnable() {  //模拟線程讓他加載
			@Override
			public void run() {
				for (int i = 0; i <= 100; i++) {
					dialog.setProgress(i);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();

		dialog.show();</span>
           
Dialog對話框之ProgressDialog(進度條),DatePickerDialog,Toast
Dialog對話框之ProgressDialog(進度條),DatePickerDialog,Toast
Dialog對話框之ProgressDialog(進度條),DatePickerDialog,Toast

DatePickerDialog

DatePickerDialog dateDialog =  new DatePickerDialog(context,new onDateSetListener(),int year,int month,int day);

dateDialog .show();

ps: 以下監聽中的參數,設定的是2015/10/15, 對話框中顯示的是11月,列印出來是10,顯示 出來的月份需要加1

Calendar calendar = Calendar.getInstance();
			int years = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			
			DatePickerDialog dateDialog = new DatePickerDialog(this,
					new OnDateSetListener() {

						@Override
						public void onDateSet(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {

						}
					}, years, month, day);
           

Toast.makeText(DialogActivity.this,"current time: " + year + "- " + monthOfYear+ "- " + dayOfMonth+1, Toast.LENGTH_SHORT).show();}}, years, month, day);dataDialog.show();

Toast

Toast.makeText(context,"objtext","Toast.LENGTH_SHORT").show();

有setview()方法,可以自定義對話框

Toast toast = new Toast(context);

View v = inflater.inflate(R.layout....);

toast.setView(v);

//下面的布局是自定義alertdialog的布局,效果同自定義alertdialog一樣

<span style="font-size:14px;">Toast toast = new Toast(DialogActivity.this);
				toast.setView(LayoutInflater.from(DialogActivity.this).inflate(
						R.layout.dialog_alert_d_layout, null));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();</span>
           

繼續閱讀