我先上效果图:
看一下activity
package com.example.administrator.mydialog;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.administrator.mydialog.dialog.CancelDialog;
import com.example.administrator.mydialog.dialog.ChoiceRegionDialog;
import com.example.administrator.mydialog.dialog.CityAppDialog;
import com.example.administrator.mydialog.dialog.DeleteShoppingDialog;
import com.example.administrator.mydialog.dialog.NZWProgressDialog;
import com.example.administrator.mydialog.util.SelectRegionUtil;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Activity activity;
private List regionList=new ArrayList<>();
private Button btn_ok,btn_exit,btn_select,btn_delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activity=MainActivity.this;
initView();
NZWProgressDialog np;
np=NZWProgressDialog.createDialog(MainActivity.this);
np.show();
}
private void initView() {
btn_ok= (Button) findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(this);
btn_exit= (Button) findViewById(R.id.btn_exit);
btn_exit.setOnClickListener(this);
btn_select= (Button) findViewById(R.id.btn_select);
btn_select.setOnClickListener(this);
btn_delete= (Button) findViewById(R.id.btn_delete);
btn_delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id=v.getId();
switch (id){
case R.id.btn_ok:
CityAppDialog cd=new CityAppDialog(MainActivity.this);
cd.show();
break;
case R.id.btn_exit:
CancelDialog cancel=new CancelDialog(MainActivity.this,activity);
cancel.exitDialog();
break;
case R.id.btn_select:
//选择区域
ChoiceRegionDialog choice=new ChoiceRegionDialog(MainActivity.this);
SelectRegionUtil rg=new SelectRegionUtil();
rg.setRegion_name("青秀区");
rg.setRegion_id("123");
rg.setCheck(true);
regionList.add(rg);
choice.creatdialog(regionList);
break;
case R.id.btn_delete:
DeleteShoppingDialog del=new DeleteShoppingDialog(MainActivity.this);
del.delDialog();
break;
}
}
}
CancelDialog.java
package com.example.administrator.mydialog.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.administrator.mydialog.R;
/**
* Created by Administrator on 2017/3/20/020.
*/
public class CancelDialog {
private Context context;
private Activity activity;
// 选择弹窗,提示,离开、不离开、保存
public CancelDialog (Context context,Activity activity){
this.context=context;
this.activity=activity;
}
public void exitDialog() {
final Dialog builder = new Dialog(context, R.style.mydialog_style);
View viewsView = LayoutInflater.from(context).inflate(R.layout.cancel_dialog, null);
TextView tv_leave = (TextView) viewsView.findViewById(R.id.tv_leave);
TextView tv_cancel = (TextView) viewsView.findViewById(R.id.tv_cancel);
Button btn_save = (Button) viewsView.findViewById(R.id.btn_save);
tv_leave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder.dismiss();
}
});
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder.dismiss();
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 保存
builder.dismiss();
}
});
builder.setContentView(viewsView);
builder.setCanceledOnTouchOutside(false);
builder.show();
}
}
ChoiceRegionDialog.java
package com.example.administrator.mydialog.dialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.example.administrator.mydialog.R;
import com.example.administrator.mydialog.adapter.AddRegionalAdapter;
import com.example.administrator.mydialog.util.SelectRegionUtil;
import java.util.List;
/**
* Created by Administrator on 2017/3/20/020.
* 这里是选择城市区域dialog
*/
public class ChoiceRegionDialog {
private Context context;
private AddRegionalAdapter mAddRegionalAdapter;
private ImageView iv_hide;
public ChoiceRegionDialog(Context context) {
this.context = context;
}
// 选择弹窗
public void creatdialog(List regionList) {
final Dialog builder = new Dialog(context, R.style.mydialog_style);
View viewsView = LayoutInflater.from(context).inflate(R.layout.add_regional_price_dialog, null);
ListView enter_item_listview = (ListView) viewsView.findViewById(R.id.add_item_listview);
iv_hide = (ImageView) viewsView.findViewById(R.id.iv_hide);
mAddRegionalAdapter = new AddRegionalAdapter(context, regionList);
enter_item_listview.setAdapter(mAddRegionalAdapter);
enter_item_listview.setDividerHeight(0);
enter_item_listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAddRegionalAdapter.setOnclickListement(new AddRegionalAdapter.OnclickListement() {
@Override
public void onclick(String adress, String region_id) {
builder.dismiss();
}
});
iv_hide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
builder.dismiss();
}
});
builder.setContentView(viewsView);
builder.show();
}
}
CityAppDialog.java
package com.example.administrator.mydialog.dialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.administrator.mydialog.R;
/**
* 自定义dialog
*
* @author 小宁
*
*/
public class CityAppDialog {
private Context context;
private Dialogcallback dialogcallback;
private Dialog dialog;
private TextView textView;
private Button btn_ok, btn_cancel;
private TextView tv_title, tv_message;
/**
* init the dialog
*
* @return
*/
public CityAppDialog(Context con) {
this.context = con;
dialog = new Dialog(context, R.style.dialog);
dialog.setContentView(R.layout.city_dialog);
dialog.setCanceledOnTouchOutside(false);
btn_ok = (Button) dialog.findViewById(R.id.btn_ok);
btn_ok.setText("切换");
btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
btn_cancel.setText("取消");
tv_title = (TextView) dialog.findViewById(R.id.tv_title);
tv_title.setText("温馨提示");
tv_message = (TextView) dialog.findViewById(R.id.tv_message);
tv_message.setText("很抱歉,您所在的地区“广东省”不在,菜大蔬的配送范围,应用将为您切换至“广西南宁”为默认地址");
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
/**
* 设定一个interfack接口,使mydialog可以處理activity定義的事情
*
* @author sfshine
*
*/
public interface Dialogcallback {
public void dialogdo(String string);
}
public void setDialogCallback(Dialogcallback dialogcallback) {
this.dialogcallback = dialogcallback;
}
/**
* @category Set The Content of the TextView
*/
public void setContent(String content) {
textView.setText(content);
}
/**
* Get the Text of the EditText
*/
public String getText() {
// return editText.getText().toString();
return null;
}
public void show() {
dialog.show();
}
public void hide() {
dialog.hide();
}
public void dismiss() {
dialog.dismiss();
}
}
DeleteShoppingDialog.java
package com.example.administrator.mydialog.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.administrator.mydialog.R;
public class DeleteShoppingDialog {
private Context context;
private Activity activity;
public DeleteShoppingDialog( Context context) {
this.context = context;
}
// 删除商品dialog
public void delDialog() {
final Dialog builder = new Dialog(context, R.style.mydialog_style);
View viewsView = LayoutInflater.from(context).inflate(R.layout.delete_dialog, null);
Button btn_cancel = (Button) viewsView.findViewById(R.id.btn_cancel);
Button btn_save = (Button) viewsView.findViewById(R.id.btn_save);
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 取消
builder.dismiss();
}
});
btn_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (delShopping != null) {
delShopping.delItem();
}
builder.dismiss();
}
});
builder.setContentView(viewsView);
builder.setCanceledOnTouchOutside(false);
builder.show();
}
public interface DeleteListement {
public void delItem();
}
public DeleteListement delShopping;
public void setdel(DeleteListement del) {
this.delShopping = del;
}
}
package com.example.administrator.mydialog.dialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import com.example.administrator.mydialog.R;
public class NZWProgressDialog extends Dialog {
// private Context context = null;
private static NZWProgressDialog customProgressDialog = null;
public static boolean canCancelTouchOuside;
private Context contextmain;
public NZWProgressDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
protected NZWProgressDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
public NZWProgressDialog(Context context) {
super(context);
this.contextmain = context;
// TODO Auto-generated constructor stub
}
public static NZWProgressDialog createDialog(final Context context) {
customProgressDialog = new NZWProgressDialog(context,
R.style.ZHSProgressDialog);
View view = LayoutInflater.from(context).inflate(
R.layout.progress_dialog_layout, null);
// customProgressDialog.setCanceledOnTouchOutside(canCancelTouchOuside);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "点击对话框", Toast.LENGTH_SHORT).show();
if (customProgressDialog != null
&& customProgressDialog.isShowing()
&& canCancelTouchOuside) {
customProgressDialog.dismiss();
}
}
});
customProgressDialog.setContentView(view);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customProgressDialog;
}
@Override
public void setCanceledOnTouchOutside(boolean cancel) {
// TODO Auto-generated method stub
super.setCanceledOnTouchOutside(cancel);
canCancelTouchOuside = cancel;
// setCancelable(cancel);
}
}
AddRegionalAdapter.java
package com.example.administrator.mydialog.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.example.administrator.mydialog.R;
import com.example.administrator.mydialog.util.SelectRegionUtil;
import java.util.HashMap;
import java.util.List;
/**
* 增加收货地址适配器
*
* @author SKLM
*
*/
public class AddRegionalAdapter extends BaseAdapter {
private Context context;
private List dataList;
// 用于记录每个RadioButton的状态,并保证只可选一个
HashMap states = new HashMap();
public AddRegionalAdapter(Context context, List dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressWarnings("deprecation")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.add_regional_item, null);
viewHolder = new ViewHolder();
viewHolder.rb_select_city = (RadioButton) convertView.findViewById(R.id.rb_select_city);
viewHolder.tv_region = (TextView) convertView.findViewById(R.id.tv_region);
viewHolder.llt_select = (LinearLayout) convertView.findViewById(R.id.llt_select);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv_region.setText(dataList.get(position).getRegion_name());
viewHolder.rb_select_city.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 重置,确保最多只有一项被选中
for (String key : states.keySet()) {
states.put(key, false);
}
states.put(String.valueOf(position), viewHolder.rb_select_city.isChecked());
if (onclickListement != null) {
onclickListement.onclick(dataList.get(position).getRegion_name(),
dataList.get(position).getRegion_id());
}
notifyDataSetChanged();
}
});
boolean res = false;
if (states.get(String.valueOf(position)) == null || states.get(String.valueOf(position)) == false) {
res = false;
states.put(String.valueOf(position), false);
} else
res = true;
viewHolder.rb_select_city.setChecked(res);
return convertView;
}
public interface OnclickListement {
void onclick(String adress, String region_id);
}
public OnclickListement onclickListement;
public void setOnclickListement(OnclickListement onclickListement) {
this.onclickListement = onclickListement;
}
static class ViewHolder {
public RadioButton rb_select_city;
public TextView tv_region;
public LinearLayout llt_select;
}
// 回调
public AddDataCallBack adddatacllback;
public interface AddDataCallBack {
public void interfcasecallback(int exttype, int positons);
}
public void getcallback(AddDataCallBack adddatacllback) {
this.adddatacllback = adddatacllback;
}
}
SelectRegionUtil.java
package com.example.administrator.mydialog.util;
public class SelectRegionUtil {
String region_id = "3950";
String region_name;
boolean isCheck;// true为选择
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean isCheck) {
this.isCheck = isCheck;
}
public String getRegion_id() {
return region_id;
}
public void setRegion_id(String region_id) {
this.region_id = region_id;
}
activity_main.xml
add_regional_item.xml
add_regional_price_dialog.xml
cancel_dialog.xml
mydialog.xml
progress_dialog_layout.xml
colors.xml
#3F51B5
#303F9F
#FF4081
#f2f2f2
#fafafa
#FF0000
#0aa95e
#ffffff
#0da75e
city_bac.xml
exit_cancelbtn_gb.xml
exit_okbtn_gb.xml
persons_material_gb.xml
select_price_city.xml
select_price_region.xml
update_cancelbtn_gb.xml
update_okbtn_gb.xml