編輯:Android資訊
AlterDialog在Android開發中使用也比較頻繁,本文就介紹了AlterDialog的幾種對話框使用方式及代碼解析。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.icon); builder.setTitle("你確定要離開嗎?"); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //這裡添加點擊確定後的邏輯 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //這裡添加點擊確定後的邏輯 } }); builder.create().show();
String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this); builder.setTitle("列表選擇框"); builder.setItems(mItems, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //點擊後彈出窗口選擇了第幾項 } }); builder.create().show();
int mSingleChoiceID = -1; builder.setIcon(R.drawable.icon); builder.setTitle("單項選擇"); builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mSingleChoiceID = whichButton; } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { if(mSingleChoiceID > 0) { // 你選擇的是" + mSingleChoiceID; } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); builder.create().show();
ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); builder.setIcon(R.drawable.icon); builder.setTitle("多項選擇"); builder.setMultiChoiceItems(mItems, new boolean[]{false, false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) { if(isChecked) { MultiChoiceID.add(whichButton); // 你選擇的為:mItems[whichButton] }else { MultiChoiceID.remove(whichButton); } } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String str = ""; int size = MultiChoiceID.size(); for (int i = 0 ;i < size; i++) { <span > </span> str+= mItems[MultiChoiceID.get(i)] + ", "; } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); builder.create().show();
View view = LayoutInflater.from(this).inflate(R.layout.custom, null); builder.setIcon(R.drawable.icon); builder.setTitle("自定義輸入框"); builder.setView(<span >view</span>); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName); EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord); // userName.getText().toString() + password.getText().toString(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); builder.create().show();
對於我這樣一個Android初級開發者來說,自定義View一直是一個遙不可及的東西,每次看到別人做的特別漂亮的控件,自己心裡那個癢癢啊,可是又生性懶惰,自己不肯努
Gallery能夠水平顯示其內容,一般用來浏覽圖片,被選中的選項位於中間,並且可以相應事件顯示信息。下面結合ImageSwitcher組件來實現一個通過縮略圖來浏
Android中的TTextView很強大,我們可以不僅可以設置純文本為其內容,還可以設置包含網址和電子郵件地址的內容,並且使得這些點擊可以點擊。但是我們可以捕獲
本篇博客要給大家分享的一個關於Android應用換膚的Demo,大家可以到我的github去下載demo,以後博文涉及到的代碼均會上傳到github中統一管理。