詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
ProgressDialog(精度條對話框):
1.直接調(diào)用ProgressDialog提供的靜態(tài)方法show()顯示
2.創(chuàng)建ProgressDialog,再設(shè)置對話框的參數(shù),最后show()出來
package com.example.test3; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; public class MainActivity extends Activity 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) { //設(shè)置進度條的當(dāng)前值 pd2.setProgress(progressStart); } //如果當(dāng)前大于或等于進度條的最大值,調(diào)用dismiss()方法關(guān)閉對話框 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.btn1); btn_two = (Button) findViewById(R.id.btn2); btn_three = (Button) findViewById(R.id.btn3); btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: //這里的話參數(shù)依次為,上下文,標(biāo)題,內(nèi)容,是否顯示進度,是否可以用取消按鈕關(guān)閉 ProgressDialog.show(MainActivity.this, "資源加載中", "資源加載中,請稍后...",false,true); break; case R.id.btn2: pd1 = new ProgressDialog(mContext); //依次設(shè)置標(biāo)題,內(nèi)容,是否用取消按鈕關(guān)閉,是否顯示進度 pd1.setTitle("軟件更新中"); pd1.setMessage("軟件正在更新中,請稍后..."); pd1.setCancelable(true); //這里是設(shè)置進度條的風(fēng)格,HORIZONTAL是水平進度條,SPINNER是圓形進度條 pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd1.setIndeterminate(true); //調(diào)用show()方法將ProgressDialog顯示出來 pd1.show(); break; case R.id.btn3: //初始化屬性 progressStart = 0; add = 0; //依次設(shè)置一些屬性 pd2 = new ProgressDialog(MainActivity.this); pd2.setMax(MAXVALUE); pd2.setTitle("文件讀取中"); pd2.setMessage("文件加載中,請稍后..."); //這里設(shè)置為不可以通過按取消按鈕關(guān)閉進度條 pd2.setCancelable(false); pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //這里設(shè)置的是是否顯示進度,設(shè)為false才是顯示的哦! pd2.setIndeterminate(false); pd2.show(); //這里的話新建一個線程,重寫run()方法, new Thread() { public void run() { while(progressStart < MAXVALUE) { //這里的算法是決定進度條變化的,可以按需要寫 progressStart = 2 * usetime() ; //把信息碼發(fā)送給handle讓更新界面 hand.sendEmptyMessage(123); } } }.start(); break; } } //這里設(shè)置一個耗時的方法: private int usetime() { add++; try{ Thread.sleep(100); }catch (InterruptedException e) { e.printStackTrace(); } return add; } }
2.Date/TimePickerDialog只是供用戶來選擇日期時間,對于android系統(tǒng)的系統(tǒng)時間, 日期沒有任何影響
他們兩個的構(gòu)造方法非常相似: DatePickerDialog(上下文;DatePickerDialog.OnDateSetListener()監(jiān)聽器;年;月;日)
TimePickerDialog(上下文;TimePickerDialog.OnTimeSetListener()監(jiān)聽器;小時,分鐘,是否采用24小時制)
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; } } }
最后一個用于顯示信息的UI控件——PopupWindow(懸浮框),如果你想知道 他長什么樣子,你可以打開你手機的QQ,長按列表中的某項,這個時候后彈出一個黑色的小 對話框,這種就是PopupWindow了,和AlertDialog對話框不同的是,他的位置可以是隨意的; 另外AlertDialog是非堵塞線程的,而PopupWindow則是堵塞線程的
1)幾個常用的構(gòu)造方法
我們在文檔中可以看到,提供給我們的PopupWindow的構(gòu)造方法有九種之多,這里只貼實際 開發(fā)中用得較多的幾個構(gòu)造方法:
public PopupWindow (Context context) public PopupWindow(View contentView, int width, int height) public PopupWindow(View contentView) public PopupWindow(View contentView, int width, int height, boolean focusable)
參數(shù)就不用多解釋了吧,contentView是PopupWindow顯示的View,focusable是否顯示焦點
2)常用的一些方法
下面介紹幾個用得較多的一些方法,其他的可自行查閱文檔:
setContentView(View contentView):設(shè)置PopupWindow顯示的View
getContentView():獲得PopupWindow顯示的View
showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y): 相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移 PS:parent這個參數(shù)只要是activity中的view就可以了!
setWidth/setHeight:設(shè)置寬高,也可以在構(gòu)造方法那里指定好寬高, 除了可以寫具體的值,還可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height屬性直接和第一層View相對應(yīng)。
setFocusable(true):設(shè)置焦點,PopupWindow彈出后,所有的觸屏和物理按鍵都由PopupWindows 處理。其他任何事件的響應(yīng)都必須發(fā)生在PopupWindow消失之后,(home 等系統(tǒng)層面的事件除外)。 比如這樣一個PopupWindow出現(xiàn)的時候,按back鍵首先是讓PopupWindow消失,第二次按才是退出 activity,準確的說是想退出activity你得首先讓PopupWindow消失,因為不并是任何情況下按back PopupWindow都會消失,必須在PopupWindow設(shè)置了背景的情況下 。
setAnimationStyle(int):設(shè)置動畫效果
public class MainActivity extends Activity { private Button btn_show; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; btn_show = (Button) findViewById(R.id.btn_show); btn_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initPopWindow(v); } }); } private void initPopWindow(View v) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false); Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi); Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe); //1.構(gòu)造一個PopupWindow,參數(shù)依次是加載的View,寬高 final PopupWindow popWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popWindow.setAnimationStyle(R.anim.anim_pop); //設(shè)置加載動畫 //這些為了點擊非PopupWindow區(qū)域,PopupWindow會消失的,如果沒有下面的 //代碼的話,你會發(fā)現(xiàn),當(dāng)你把PopupWindow顯示出來了,無論你按多少次后退鍵 //PopupWindow并不會關(guān)閉,而且退不出程序,加上下述代碼可以解決這個問題 popWindow.setTouchable(true); popWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; // 這里如果返回true的話,touch事件將被攔截 // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點擊外部區(qū)域無法dismiss } }); popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要為popWindow設(shè)置一個背景才有效 //設(shè)置popupWindow顯示的位置,參數(shù)依次是參照View,x軸的偏移量,y軸的偏移量 popWindow.showAsDropDown(v, 50, 0); //設(shè)置popupWindow里的按鈕的事件 btn_xixi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點擊了嘻嘻~", Toast.LENGTH_SHORT).show(); } }); btn_hehe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點擊了呵呵~", Toast.LENGTH_SHORT).show(); popWindow.dismiss(); } }); } }
相關(guān)文章
Android App開發(fā)中自定義View和ViewGroup的實例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實例教程,分別介紹了進度條和圖片上傳并排列的例子,效果很好很強大,需要的朋友可以參考下2016-05-05Jetpack?Compose?的新型架構(gòu)?MVI使用詳解
這篇文章主要介紹了Jetpack?Compose?的新型架構(gòu)?MVI使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09Android在listview添加checkbox實現(xiàn)原理與代碼
Android在listview添加checkbox如何實現(xiàn)一直都是新手朋友們的頭疼問題,接下來為您詳細介紹實現(xiàn)方法,感興趣的朋友可以了解下2013-01-01Android BSearchEdit 搜索結(jié)果選擇框的實例代碼
EditText搜索結(jié)果下拉框、自動or回調(diào)模式、可diy、使用超簡便。這篇文章主要介紹了Android BSearchEdit 搜索結(jié)果選擇框的實例代碼,需要的朋友可以參考下2019-10-10