編輯:關於Android編程
在android的編程中,對話框的用處是非常多的,然而,有時對對話框的需求也是不一樣的,那麼現在,就總結對話框的種類,以及事件的處理形勢。
1、簡單的對話框
[java]
public void showDialog(){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("標題")
.setMessage("消息")
.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("不好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
public void showDialog(){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("標題")
.setMessage("消息")
.setPositiveButton("好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("不好", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
2、帶編輯框的對話框
[java]
public void showDialog(){
EditText et = new EditText(this);// 單獨出來,方便取用戶輸入的數據。
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(et)
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}})
.create();
dialog.show();
}
public void showDialog(){
EditText et = new EditText(this);// 單獨出來,方便取用戶輸入的數據。
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(et)
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}})
.create();
dialog.show();
}
3、含單選鈕的對話框
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取數據
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("單選框,請選擇")
.setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() {
// 3 代表默認選中str的index=3的那個單選框
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取數據
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("單選框,請選擇")
.setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() {
// 3 代表默認選中str的index=3的那個單選框
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
4、含多選框的對話框
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取數據
boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("多選框,請選擇")
.setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.e("DIYDialogActivity", "showDialog->" +
"you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取數據
boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("多選框,請選擇")
.setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.e("DIYDialogActivity", "showDialog->" +
"you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
5、含列表的對話框
[java]
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取數據
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("列表框,供選擇")
.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog---------------------you select " + which);
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
public void showDialog(){
final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
// 單獨列出來方便之後取數據
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("列表框,供選擇")
.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("DIYDialogActivity", "showDialog---------------------you select " + which);
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
dialog.show();
}
6、自定義的對話框
[java]
public void showDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));
// 可指定對話框的背景色彩那些
Dialog dialog = new AlertDialog.Builder(this)
.setView(layout)
.create();
// 設置對話框的出現位置,借助於window對象
Window win = dialog.getWindow();
//win.setGravity(Gravity.CENTER);
//win.setGravity(Gravity.BOTTOM);
//win.setGravity(Gravity.TOP);
win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話框時,底部窗體,不變暗。
WindowManager.LayoutParams lp = win.getAttributes();
lp.x = -200;// x=0,y=0時,顯示位置是屏幕中心。
lp.y = 0 ;
lp.alpha = 0.6f;// 對話框的透明度
win.setAttributes(lp);
Button but = (Button) layout.findViewById(R.id.but_positive);
but.requestFocus();
dialog.show();
}
public void showDialog(){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));
// 可指定對話框的背景色彩那些
Dialog dialog = new AlertDialog.Builder(this)
.setView(layout)
.create();
// 設置對話框的出現位置,借助於window對象
Window win = dialog.getWindow();
//win.setGravity(Gravity.CENTER);
//win.setGravity(Gravity.BOTTOM);
//win.setGravity(Gravity.TOP);
win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話框時,底部窗體,不變暗。
WindowManager.LayoutParams lp = win.getAttributes();
lp.x = -200;// x=0,y=0時,顯示位置是屏幕中心。
lp.y = 0 ;
lp.alpha = 0.6f;// 對話框的透明度
win.setAttributes(lp);
Button but = (Button) layout.findViewById(R.id.but_positive);
but.requestFocus();
dialog.show();
}
前言:為什麼要了解系統Activity,Service,,BroadCastReceiver,ContentProvider的啟動流程,這是一個對於即將理解插件中的四大組
ImageSelector 簡介Android自定義相冊,實現了拍照、圖片選擇(單選/多選)、ImageLoader無綁定 任由開發者選擇https://github.c
在上一篇博文的最後,我說要寫一個下拉刷新的ListView和RecyclerView,並且可以直接使用QQheader本篇博文准備講如何實現下拉刷新和上拉加載,寫的比較
最近項目實現下面的圖示的效果,本來想用listview+gridview實現,但是貌似挺麻煩的於是就用flowlayout 來addview實現添加伸縮的效果,實現也比較