常見對話框AlertDialog如何理解

今天就跟大家聊聊有關(guān)常見對話框AlertDialog如何理解,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計,臺安網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:臺安等地區(qū)。臺安做網(wǎng)站價格咨詢:18980820575

在Android應(yīng)用中,有多種對話框:Dialog、AlertDialog、ProgressDialog、時間、日期等對話框。

(1)Dialog類,是一切對話框的基類,需要注意的是,Dialog類雖然可以在界面上顯示,但是并非繼承與習(xí)慣的View類,而是直接從java.lang.Object開始構(gòu)造出來的,類似于Activity,Dialog也是有生命周期的,它的生命周期由Activity來維護。Activity負責(zé)生產(chǎn),保存,回復(fù)它,在生命周期的每個階段都有一些回調(diào)函數(shù)供系統(tǒng)方向調(diào)用。

(2)AlertDialog是Dialog的一個直接子類,AlertDialog也是Android系統(tǒng)當中最常用的對話框之一。一個AlertDialog可以有兩個Button或3個Button,可以對一個AlertDialog設(shè)置title和message.不能直接通過AlertDialog的構(gòu)造函數(shù)來生成一個AlertDialog.一般生成AlertDialog的時候都是通過它的一個內(nèi)部靜態(tài)類AlertDialog.builder來構(gòu)造的。

(3)顧名思義,這個Dialog負責(zé)給用戶顯示進度的相關(guān)情況,它是AlertDialog的一個子類。

本章我們著重講解一下AlertDialog!

AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過new一個AlertDialog來創(chuàng)建出一個AlertDialog。

要創(chuàng)建一個AlertDialog,就要用到AlertDialog.Builder中的create()方法。

使用AlertDialog.Builder創(chuàng)建對話框需要了解以下幾個方法:

setTitle :為對話框設(shè)置標題
  setIcon :為對話框設(shè)置圖標
  setMessage:為對話框設(shè)置內(nèi)容
  setView : 給對話框設(shè)置自定義樣式
  setItems :設(shè)置對話框要顯示的一個list,一般用于顯示幾個命令時
  setMultiChoiceItems :用來設(shè)置對話框顯示一系列的復(fù)選框
  setNeutralButton :普通按鈕

setPositiveButton :給對話框添加"Yes"按鈕“確定”
  setNegativeButton :對話框添加"No"按鈕  “取消”
  create : 創(chuàng)建對話框
  show :顯示對話框

一、簡單對話框

Dialog類雖然可以在界面上顯示,但是并非繼承與習(xí)慣的View類,而是直接從java.lang.Object開始構(gòu)造出來的。

1、打開“src/com.genwoxue.alertdialog_a/MainActivity.java”文件。

然后輸入以下代碼:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過new一個AlertDialog來創(chuàng)建出一個AlertDialog
//要創(chuàng)建一個AlertDialog,就要用到AlertDialog.Builder中的create()方法
Builder adInfo = new AlertDialog.Builder(this);
adInfo.setTitle("簡單對話框");//設(shè)置標題
adInfo.setMessage("這是一個美麗的傳說,精美的石頭會唱歌……");//設(shè)置內(nèi)容
adInfo.create();
adInfo.show();//打開app就彈出這個對話框,點擊對話框外面的對話框會消失不見
}




}

2、運行,顯示界面:

常見對話框AlertDialog如何理解

二、帶按鈕的AlertDialog

我們在執(zhí)行刪除、確認等操作時,常常在對話框中單擊按鈕,AlertDialog可以顯示3個按鈕。

1、打開“src/com.genwoxue.alertdialog_bMainActivity.java”文件。

然后輸入以下代碼:


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("確定刪除?");
dialog.setMessage("您確定刪除該條信息嗎?");
dialog.setIcon(R.drawable.ic_launcher);
//為“確定”按鈕注冊監(jiān)聽事件
dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// 根據(jù)實際情況編寫相應(yīng)代碼

}
});
//為“取消”按鈕注冊監(jiān)聽事件
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// 根據(jù)實際情況編寫相應(yīng)代碼

}
});
dialog.setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// 根據(jù)實際情況編寫相應(yīng)代碼

}
});
dialog.create();
dialog.show();
}


}

2、運行,顯示界面:

常見對話框AlertDialog如何理解

三、帶有單選按鈕、類似ListView的AlertDialog對話框

setSingleChoiceItems(CharSequence[] items, int checkedItem,final OnClickListener listener)方法來實現(xiàn)類似ListView的AlertDialog,第一個參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個參數(shù)指定默認選中項,第三個參數(shù)設(shè)置監(jiān)聽處理事件。

1、打開“src/com.genwoxue.alertdialog_c/MainActivity.java”文件。

然后輸入以下代碼:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {
//聲明選中項變量
private int selectedCityIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定義城市數(shù)組
final String[] arrayCity = new String[]{"杭州","紐約","威尼斯","北海道"};
//實例化AlertDialog對話框
Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你最喜歡哪個地方")//設(shè)置標題
.setIcon(R.drawable.ic_launcher)//設(shè)置圖標
//設(shè)置對話框顯示一個單選List,指定默認選中項,同時設(shè)置監(jiān)聽事件處理
.setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 選中項的索引保存到選中項變量
selectedCityIndex = which;

}
})
//添加取消按鈕并增加監(jiān)聽處理
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO 自動生成的方法存根

}
})
//添加確定按鈕并增加監(jiān)聽處理
.setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
//顯示選中的值
Toast.makeText(getApplicationContext(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();//.show()必須單獨寫,不能接著上面的

}
}

2、運行,顯示界面:

常見對話框AlertDialog如何理解

四、帶有復(fù)選框、類似ListView的AlertDialog對話框

setMultiChoiceItems(CharSequence[] items, boolearn[] checkedItems,final OnMultiChoiceClickListener listener)方法來實現(xiàn)類似ListView的AlertDialog,第一個參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個參數(shù)指定默認選中項,第在個參數(shù)設(shè)置監(jiān)聽處理事件。

1、打開“src/com.genwoxue.alertdialog_d/MainActivity.java”文件。

然后輸入以下代碼:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定義運動數(shù)組
final String[] arraySport = {"足球","籃球","網(wǎng)球","乒乓球"};
final boolean[] arraySportSelected = new boolean[]{false,false,false,false};
//實例化AlertDialog對話框
Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你喜歡哪些運動")//設(shè)置標題
.setIcon(R.drawable.ic_launcher)//設(shè)置圖標
//設(shè)置對話框顯示一個復(fù)選List,指定默認選中項,同時設(shè)置監(jiān)聽事件處理
.setMultiChoiceItems(arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 選中項的布爾真假保存到選中項變量
arraySportSelected[which] = isChecked;

}
})
//添加確定按鈕并增加監(jiān)聽處理
.setPositiveButton("確定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder sb = new StringBuilder();
for(int i=0;i<arraySportSelected.length;i++){
if(arraySportSelected[i]==true){
sb.append(arraySport[i]+"、");
}
}
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
}
})
//添加取消按鈕并增加監(jiān)聽處理
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO 自動生成的方法存根

}
}).create();
alertDialog.show();
}
}

看完上述內(nèi)容,你們對常見對話框AlertDialog如何理解有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

名稱欄目:常見對話框AlertDialog如何理解
標題網(wǎng)址:http://m.kartarina.com/article36/gesppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號App開發(fā)用戶體驗手機網(wǎng)站建設(shè)Google搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)
主站蜘蛛池模板: 亚洲AV无码精品色午夜在线观看| 亚洲AV中文无码字幕色三| 亚洲最大天堂无码精品区| 亚洲av午夜国产精品无码中文字| 国产乱人伦无无码视频试看 | 最新国产精品无码| 亚洲精品偷拍无码不卡av| 日日日日做夜夜夜夜无码| 无码中文字幕一区二区三区| 亚洲av无码成人黄网站在线观看| 国产午夜鲁丝片AV无码| 麻豆国产精品无码视频| 中文字幕乱码无码人妻系列蜜桃 | 中文字幕无码久久精品青草| 亚洲av永久无码一区二区三区| 亚洲av中文无码乱人伦在线r▽| 日韩精品人妻系列无码av东京 | 国产日韩精品无码区免费专区国产| 无码人妻品一区二区三区精99 | 亚洲a∨无码精品色午夜| 免费A级毛片无码专区| 国产午夜无码视频在线观看| 国产成人无码区免费网站 | 中文字幕AV中文字无码亚 | 无码尹人久久相蕉无码| 蜜芽亚洲av无码精品色午夜| 久久午夜无码免费| 亚洲Aⅴ无码专区在线观看q| 亚洲av无码av制服另类专区| 一本一道AV无码中文字幕| 红桃AV一区二区三区在线无码AV| 日日摸日日碰人妻无码| 在线看片无码永久免费aⅴ| 伊人久久大香线蕉无码麻豆| 无码av天天av天天爽| 无码精品蜜桃一区二区三区WW | 丰满日韩放荡少妇无码视频| 中文字幕久久精品无码| 无码精品国产VA在线观看| 无码精品A∨在线观看免费| 亚洲中文字幕无码久久2020|