編輯:關於android開發
在Android開發中,我們經常會需要在Android界面上彈出一些對話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對話框,AlertDialog實現方法為建造者模式。下面我們簡單模擬一個選花魁的簡單普通選項(單選)對話框,如下圖:
Layout界面代碼:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <Button 6 android:text="普通選項" 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:onClick="show2" 10 android:id="@+id/button2" /> 11 </LinearLayout>
Java功能實現代碼:
1 public class AlertDialogDemo extends AppCompatActivity {
2 @Override
3 protected void onCreate(@Nullable Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.alertdialog);
6 }
7 public void show2(View v){
8 //實例化建造者
9 AlertDialog.Builder builder = new AlertDialog.Builder(this);
10 //設置警告對話框標題
11 builder.setTitle("選花魁喽");
12 //定義警告框選擇窗體的內容(這裡用String數組)
13 final String[] beautifulGirls = {"芙蓉姐姐","鳳姐","范爺","我自己"};
14 //將數組內的元素設置到dialog的每個item中,並做事件處理(這裡用土司)
15 builder.setItems(beautifulGirls, new DialogInterface.OnClickListener() {
16 @Override
17 public void onClick(DialogInterface dialog, int which) {
18 Toast.makeText(AlertDialogDemo.this,beautifulGirls[which]+"被選中啦",Toast.LENGTH_SHORT).show();
19 }
20 });
21 //顯示對話框
22 builder.show();
23 }
24 }
Android—自定義控件實現ListView下拉刷新,androidlistview
Android—自定義控件實現ListView下拉刷新,androidlistview這篇博客為大家介紹一個android常見的功能——ListV
Android中使用Notification實現寬視圖通知欄(Notification示例二),notification大視圖
Android中使用Notification實現寬視圖通知欄(Notification示例二),notification大視圖Notification是在你的應用常規界面
Android 繪制view的小知識點,android繪制view
Android 繪制view的小知識點,android繪制view【onMeasure】 直接繼承view或ViewGroup的自定義控件需要重寫onMeasure方法並
[FastDev4Android框架開發]神器ViewGragHelper完全解析之詳解實現QQ5.X側滑酷炫效果(三十四)
[FastDev4Android框架開發]神器ViewGragHelper完全解析之詳解實現QQ5.X側滑酷炫效果(三十四) (一).前言: 這幾天正