這篇文章將為大家詳細講解有關Android中怎么實現對話框,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1 彈出普通對話框 --- 系統更新
//彈出普通對話框
public void showNormalDialog(View v) {
AlertDialog.Builder builder = new Builder(this);
//設置Dialog的圖標
builder.setIcon(R.drawable.ic_launcher);
//設置對話框的標題
builder.setTitle("更新");
//設置message
builder.setMessage("發現新版本是否更新?");
//確定按鈕 取消按鈕
builder.setPositiveButton("確定",new OnClickListener() {
/**
* 點擊確定按鈕 回調該方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務器去下載新的版本 duration單詞意思:時長
Toast.makeText(MainActivity.this, "開始下載新版本", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
/**
* 點擊取消按鈕 回調該方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務器去下載新的版本 duration單詞意思:時長
Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("下一次", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務器去下載新的版本 duration單詞意思:時長
Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();
}
});
//通過建造這老構建一個對話框
Dialog dialog = builder.create();
//顯示
dialog.show();
}2 自定義對話框-- 用戶登錄
布局文件:
user_name_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dip" android:orientation="vertical" > <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄信息" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入用戶名"/> <TextView android:id="@+id/tv_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼:" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="請輸入密碼"/> <requestFocus /> <Button android:id="@+id/btn_confirm" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_gravity="center" android:text="登錄" /> <Button android:id="@+id/btn_cancel" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_gravity="center" android:text="取消" /> </LinearLayout>
java代碼:
//自定義對話框
Dialog cus_dialog ;
public void showCustomDialog(View v){
AlertDialog.Builder builder = new Builder(this);
// 布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.user_name_dialog, null);
// 設置自定義的對話框界面
builder.setView(view);
// 獲取用戶名密碼
final EditText name = (EditText) view.findViewById(R.id.et_name);
final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);
Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自動生成的方法存根
if(name.getText().toString().trim().equals("abc")){
showToastMsg("用戶名正確");
// 對話框消失
cus_dialog.dismiss();
}
else{
showToastMsg("用戶名錯誤");
}
}
});
Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 對話框消失
cus_dialog.dismiss();
}
});
cus_dialog = builder.create();
cus_dialog.show();
}下載地址:http://www.jinhusns.com/Products/Download/?type=yhq
3 時間選擇對話框 -- 時間對話框
// 時間選擇對話框
public void showTimePickerDialog(View v){
Calendar sysDate = Calendar.getInstance();
//設置系統時間
sysDate.setTimeInMillis(System.currentTimeMillis());
int hour = sysDate.get(Calendar.HOUR_OF_DAY);
int minute = sysDate.get(Calendar.MINUTE);
TimePickerDialog time = new TimePickerDialog(this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO 自動生成的方法存根
showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);
}
}, //callBack 選擇時間后的回調方法
hour,//hourOfDay 當前系統時間
minute,//hourOfDay 當前系統時間
true);//是否24小時制
time.show();
}4 進度條對話框 -- 信息加載..
/**
* 進度條對話框
* @param v
*/
public void showProgressDialog(View v){
final ProgressDialog progress = new ProgressDialog(this);
progress.setProgress(R.drawable.img2);
progress.setTitle("標題");
progress.setMessage("加載中...");
//樣式1 進度條樣式
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//樣式2 有加載圖標
//progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//最大
progress.setMax(100);
//當前
progress.setProgress(50);
progress.setButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自動生成的方法存根
showToastMsg("確定按鈕");
}
});
progress.setButton2("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自動生成的方法存根
showToastMsg("取消按鈕");
}
});
progress.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新進度條的進度,可以在子線程中更新進度條進度
progress.incrementProgressBy(5);
// progress.incrementSecondaryProgressBy(10);//二級進度條更新方式
i += 5;
} catch (Exception e) {
// TODO: handle exception
}
}
// 在進度條走完時刪除Dialog
progress.dismiss();
}
}).start();
}5 popuWindow對話框
Button btn_popu;
//popuWindow對話框
public void showPopuWindow(View v){
btn_popu = (Button) v;
// 設置布局
View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
PopupWindow window = new PopupWindow(this);
window.setContentView(view);
window.setWidth(360);
window.setHeight(200);
int[] location = new int[2];
// 獲取按鈕坐標
btn_popu.getLocationInWindow(location);
window.setFocusable(true);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));
window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );
//showToastMsg("" + (location[0]+ btn_popu.getWidth())+" "+ (location[1] + btn_popu.getHeight() / 2));
ImageView img_start = (ImageView) view.findViewById(R.id.img_start);
img_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自動生成的方法存根
showToastMsg("點擊了啟動");
}
});
}關于Android中怎么實現對話框就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。