




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第Android開發(fā)之AlertDialog實現(xiàn)彈出對話框本文實例為大家分享了Android開發(fā)之AlertDialog實現(xiàn)彈出對話框的具體代碼,供大家參考,具體內(nèi)容如下
我們在xml中添加一個按鈕用來喚出對話框:
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
Button
android:text="顯示對話框"
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"/
/LinearLayout
然后在java代碼中編寫點擊事件的響應:
packagecom.example.myalertdialog;
importandroidx.appcompat.app.AlertDialog;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
publicclassMainActivityextendsAppCompatActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoiddisplay(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
.setTitle("對話框")
.setMessage("Hello")
.create()
.show();
}
}
先聲明一個構造器對象builder:AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
之后就可以用該構造器的各種方法設置對話框的屬性:
builder.setIcon(inticonId);添加圖標
builder.setMessage(CharSequencemessage);添加消息
builder.setTitle(CharSequencetitle);添加標題
builder.setView(Viewview);設置自定義布局
builder.create();創(chuàng)建對話框
builder.show();顯示對話框
上面的這些函數(shù)都是可以鏈式調(diào)用的(詳見基本框架),不過由于setXXX是Builder函數(shù),create函數(shù)返回Dialog變量,而show是void函數(shù),所以這三類函數(shù)的順序不能交換,setXXX函數(shù)的內(nèi)部順序可交換。
運行基本框架中的代碼就可以得到一個簡單的彈出對話框:
常見的對話框一般還有按鈕,包含確認、取消等按鈕,我們也可以在java代碼中設置并聲明點擊事件:
packagecom.example.myalertdialog;
importandroidx.appcompat.app.AlertDialog;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.content.DialogInterface;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
publicclassMainActivityextendsAppCompatActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoiddisplay(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
.setTitle("對話框")
.setMessage("Hello")
.setPositiveButton("OK",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialogInterface,inti){
Log.e("ShadyPi","點擊確認");
}
})
.setNegativeButton("Cancel",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialogInterface,inti){
Log.e("ShadyPi","點擊取消");
}
})
.setNeutralButton("middle",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialogInterface,inti){
Log.e("ShadyPi","點擊中性");
}
})
.create()
.show();
}
}
點擊對應按鈕可以看到事件被觸發(fā):
這三個按鈕可以根據(jù)自己的需要進行取舍與設置。
設置自定義布局
在layout文件夾中新建資源文件:
隨便添加一點ImageView和TextView:
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
android:orientation="horizontal"
ImageView
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/
TextView
android:text="Android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/
/LinearLayout
在java代碼中利用該資源文件生成一個View:
Viewdialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);
之后就可以將對話框的步距設置為該View了:
.setView(dialog_view)
MainActivity.java完整代碼:
packagecom.example.myalertdialog;
importandroidx.appcompat.app.AlertDialog;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.content.DialogInterface;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
publicclassMainActivityextendsAppCompatActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoiddisplay(Viewview){
Viewdialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
.setTitle("對話框")
.setMessage("Hello")
.setPositiveButton("OK",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialogInterface,inti){
Log.e("ShadyPi","點擊確認");
}
})
.setNegativeButton("Cancel",newDialogInterface.OnClickListener(){
@O
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人工智能領域人才引進措施
- 特殊學生遠程教育幫扶措施
- 機電安裝施工進度計劃和工期保證措施
- 2025高一下學期物理作業(yè)布置計劃
- 混凝土工程施工質(zhì)量驗收措施
- 西師版五年級下冊數(shù)學學科競賽計劃
- 2025幼兒園保教安全管理計劃
- 高校英語興趣小組比賽活動計劃
- 中華師道視角的智慧校園建設范文
- 2025年職業(yè)技術院校教師培訓計劃
- 降低住院患者壓力性損傷發(fā)生率
- 三年級上冊《貴州省生態(tài)文明城市建設讀本》小學中年級版教案
- 廣東省韶關市2023-2024學年八年級下學期期末歷史試題(解析版)
- 08D800-8民用建筑電氣設計與施工防雷與接地
- 福建省醫(yī)療機構三伏貼醫(yī)療技術備案表
- JBT 14645-2023 低溫裝置用密封墊片 (正式版)
- DZ∕T 0213-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 石灰?guī)r、水泥配料類(正式版)
- 2024年廣東省香港大學深圳醫(yī)院財務部崗位招聘歷年高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 中國歷史地理智慧樹知到期末考試答案章節(jié)答案2024年北京大學
- 上冊《漢語拼音單韻母-a-o-e-i-u-ü》教學課件
- 潑水節(jié)節(jié)日介紹
評論
0/150
提交評論