Android開發(fā)入門之對話框簡單用法
本文實例講述了Android開發(fā)入門之對話框簡單用法。分享給大家供大家參考,具體如下:
注:本文只是一個學習筆記 用以記錄自己學到哪了
1.獲得AlertDialog的靜態(tài)內(nèi)部類Builder對象,由此類來創(chuàng)建對話框
2.通過Builder對象設置對話框的標題 按鈕以及按鈕響應的事件
3.調(diào)用Builder的Create()方法創(chuàng)建對話框
4.調(diào)用AlertDialog的show()方法顯示對話框
main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/MyTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="創(chuàng)建Alert對話框" /> </LinearLayout>
MainActivity文件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)findViewById(R.id.MyTextView);
myButton = (Button)findViewById(R.id.myButton);
//添加AlertDialog.Builder對象
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//為activity中按鈕添加按鈕事件
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
builder.setTitle("您確定要刪除此條信息?").
//設置確定按鈕
setPositiveButton("Yes", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
myTextView.setText("刪除成功");
}
}).
//設置取消按鈕
setNegativeButton("No", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
myTextView.setText("取消刪除");
}
});
//創(chuàng)建對話框
AlertDialog alertDialog = builder.create();
//顯示對話框
alertDialog.show();
}
});
}
}
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android編程之a(chǎn)ctivity操作技巧總結》及《Android視圖View技巧總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
淺談Android Studio如何Debug對應so文件C/C++代碼
本篇文章主要介紹了淺談Android Studio如何Debug對應so文件C/C++代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Android冷啟動實現(xiàn)app秒開的實現(xiàn)代碼
本篇文章主要介紹了Android冷啟動實現(xiàn)app秒開的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
android studio編譯jar包或者aar包的方法教程詳解
這篇文章主要介紹了android studio編譯jar包或者aar包的方法教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android 利用反射+try catch實現(xiàn)sdk按需引入依賴庫的方法
這篇文章主要介紹了Android 利用反射+try catch來實現(xiàn)sdk按需引入依賴庫,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Android中ScrollView嵌套GridView顯示不全解決方法
這篇文章主要介紹了Android中ScrollView嵌套GridView顯示不全解決方法的相關資料,需要的朋友可以參考下2017-04-04
Android自定義DataTimePicker日期時間選擇器使用詳解
這篇文章主要為大家詳細介紹了Android自定義DataTimePicker日期時間選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09

