AlertDialog中新增listview以及幾種進度dialog的對比
阿新 • • 發佈:2019-02-13
private Context context;
private TextView tv1,tv2,tv3,tv4;
private LinearLayout ll_main;
List<Map<String, String>> nameList = new ArrayList<Map<String, String>>();// 建立一個數組儲存listview上顯示的資料
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initview();
}
private void initview() {
// TODO Auto-generated method stub
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
ll_main = (LinearLayout) findViewById(R.id.ll_main);
for (int m = 0; m < 10; m++) {// initData為一個list型別的資料來源
Map<String, String> nameMap = new HashMap<String, String>();
nameMap.put("name", "關曉彤" + m);
nameList.add(nameMap);
}
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog1();
}
});
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog2();
}
});
tv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog3();
}
});
tv4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog4();
}
});
}
protected void showDialog4() {
// TODO Auto-generated method stub
SelectDialog selectDialog = new SelectDialog(this,R.style.dialog);//建立Dialog並設定樣式主題
Window win = selectDialog.getWindow();
WindowManager.LayoutParams lp =win.getAttributes();//顯示位置
lp.x=300;//設定x座標
lp.y=120;//設定y座標
win.setAttributes(lp);
selectDialog.setCanceledOnTouchOutside(false);//設定點選Dialog外部任意區域關閉Dialog
selectDialog.show();
}
protected void showDialog3() {
// TODO Auto-generated method stub
LinearLayout linearLayoutMain = new LinearLayout(context);// 自定義一個佈局檔案
linearLayoutMain.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ListView listView = new ListView(context);// this為獲取當前的上下文
listView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, 300));
listView.setFadingEdgeLength(0);
SimpleAdapter adapter = new SimpleAdapter(context, nameList,
R.layout.item_list, new String[] { "name" },
new int[] { R.id.tv_name });
listView.setAdapter(adapter);
linearLayoutMain.addView(listView);// 往這個佈局中加入listview
//ll_main.addView(listView);
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("請選擇條目")
.setView(linearLayoutMain)// 在這裡把寫好的這個listview的佈局載入dialog中
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
dialog.setCanceledOnTouchOutside(false);// 使除了dialog以外的地方不能被點選
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Window w=dialog.getWindow();
//WindowManager.LayoutParams lp =w.getAttributes();//顯示位置
//lp.x=0;
//lp.y=0;
//w.setAttributes(lp);
dialog.show();
listView.setOnItemClickListener(new OnItemClickListener() {// 響應listview中的item的點選事件
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(context, nameList.get(arg2).get("name"), 0).show();
dialog.cancel();
}
});
}
protected void showDialog2() {
// TODO Auto-generated method stub
CustomProgressDialog mProgressDialog = CustomProgressDialog
.createDialog(context);
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
return;
}
mProgressDialog.show();
}
public static class CustomProgressDialog extends Dialog {
private static CustomProgressDialog customProgressDialog = null;
public CustomProgressDialog(Context paramContext) {
super(paramContext);
}
public CustomProgressDialog(Context paramContext, int paramInt) {
super(paramContext, paramInt);
}
public static CustomProgressDialog createDialog(Context paramContext) {
customProgressDialog = new CustomProgressDialog(paramContext,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.im_progress_dialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
customProgressDialog.setCanceledOnTouchOutside(false);
return customProgressDialog;
}
}
public class SelectDialog extends AlertDialog{
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slt_cnt_type);
}
}
protected void showDialog1() {
// TODO Auto-generated method stub
ProgressDialog progress = new ProgressDialog(context);
progress.setMessage("正在載入...");
progress.setCanceledOnTouchOutside(false);
progress.show();
// progress.dismiss();
}
private TextView tv1,tv2,tv3,tv4;
private LinearLayout ll_main;
List<Map<String, String>> nameList = new ArrayList<Map<String, String>>();// 建立一個數組儲存listview上顯示的資料
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initview();
}
private void initview() {
// TODO Auto-generated method stub
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
ll_main = (LinearLayout) findViewById(R.id.ll_main);
for (int m = 0; m < 10; m++) {// initData為一個list型別的資料來源
Map<String, String> nameMap = new HashMap<String, String>();
nameMap.put("name", "關曉彤" + m);
nameList.add(nameMap);
}
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog1();
}
});
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog2();
}
});
tv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog3();
}
});
tv4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog4();
}
});
}
protected void showDialog4() {
// TODO Auto-generated method stub
SelectDialog selectDialog = new SelectDialog(this,R.style.dialog);//建立Dialog並設定樣式主題
Window win = selectDialog.getWindow();
WindowManager.LayoutParams lp =win.getAttributes();//顯示位置
lp.x=300;//設定x座標
lp.y=120;//設定y座標
win.setAttributes(lp);
selectDialog.setCanceledOnTouchOutside(false);//設定點選Dialog外部任意區域關閉Dialog
selectDialog.show();
}
protected void showDialog3() {
// TODO Auto-generated method stub
LinearLayout linearLayoutMain = new LinearLayout(context);// 自定義一個佈局檔案
linearLayoutMain.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ListView listView = new ListView(context);// this為獲取當前的上下文
listView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, 300));
listView.setFadingEdgeLength(0);
SimpleAdapter adapter = new SimpleAdapter(context, nameList,
R.layout.item_list, new String[] { "name" },
new int[] { R.id.tv_name });
listView.setAdapter(adapter);
linearLayoutMain.addView(listView);// 往這個佈局中加入listview
//ll_main.addView(listView);
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("請選擇條目")
.setView(linearLayoutMain)// 在這裡把寫好的這個listview的佈局載入dialog中
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
dialog.setCanceledOnTouchOutside(false);// 使除了dialog以外的地方不能被點選
//dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Window w=dialog.getWindow();
//WindowManager.LayoutParams lp =w.getAttributes();//顯示位置
//lp.x=0;
//lp.y=0;
//w.setAttributes(lp);
dialog.show();
listView.setOnItemClickListener(new OnItemClickListener() {// 響應listview中的item的點選事件
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(context, nameList.get(arg2).get("name"), 0).show();
dialog.cancel();
}
});
}
protected void showDialog2() {
// TODO Auto-generated method stub
CustomProgressDialog mProgressDialog = CustomProgressDialog
.createDialog(context);
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
return;
}
mProgressDialog.show();
}
public static class CustomProgressDialog extends Dialog {
private static CustomProgressDialog customProgressDialog = null;
public CustomProgressDialog(Context paramContext) {
super(paramContext);
}
public CustomProgressDialog(Context paramContext, int paramInt) {
super(paramContext, paramInt);
}
public static CustomProgressDialog createDialog(Context paramContext) {
customProgressDialog = new CustomProgressDialog(paramContext,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.im_progress_dialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
customProgressDialog.setCanceledOnTouchOutside(false);
return customProgressDialog;
}
}
public class SelectDialog extends AlertDialog{
public SelectDialog(Context context, int theme) {
super(context, theme);
}
public SelectDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slt_cnt_type);
}
}
protected void showDialog1() {
// TODO Auto-generated method stub
ProgressDialog progress = new ProgressDialog(context);
progress.setMessage("正在載入...");
progress.setCanceledOnTouchOutside(false);
progress.show();
// progress.dismiss();
}