編輯:關於Android編程
本文實例講述了Android開發入門之對話框簡單用法。分享給大家供大家參考,具體如下:
注:本文只是一個學習筆記 用以記錄自己學到哪了
1.獲得AlertDialog的靜態內部類Builder對象,由此類來創建對話框
2.通過Builder對象設置對話框的標題 按鈕以及按鈕響應的事件
3.調用Builder的Create()方法創建對話框
4.調用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="創建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("取消刪除");
}
});
//創建對話框
AlertDialog alertDialog = builder.create();
//顯示對話框
alertDialog.show();
}
});
}
}
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android編程之activity操作技巧總結》及《Android視圖View技巧總結》
希望本文所述對大家Android程序設計有所幫助。
Android仿天貓下拉刷新自定義控件
1、概述控件基於android-Ultra-Pull-to-Refresh做的header定制,繼承PtrFrameLayout,把事件分發給裡面的RadioGroup,
Android 自定義控件之可多選課程日歷CalendarView
效果圖開發環境IDE版本:AndroidStudio2.0物理機版本:Win7旗艦版(64位)前言最近的項目中用到了一個課程選擇的日歷View,於是在網上搜了搜自定義日歷
Android項目中包名的修改
通常修改包名時會造成R文件錯誤,並且有時帶有原因不明的Manifest文件中多處文本混亂。 所以,將目前認為最為簡潔方便的修改包名流程記錄如下: 假設我們目前的包名為co
android開發使用SQLite之寫日記
使用數據庫實現對數據的存儲。 下面上一個小例子,寫日記。 效果如下: 當LIstView中沒有數據顯示時,我們需要告訴用戶沒有數據.