Android 使用DialogFragment 对话框实现
2014-12-12 16:40
393 查看
这里使用了 DialogFragment 来实现一个对话框 虽然 直接使用 AlertDialog 就可以很容易的实现 为了复用代码 和使用Fragment 在项目里还是决定使用 Fragment来实现该功能
具体实现类如下:
1 继承 DialogFragment 在onCreateDialog 方法中返回 一个Dialog 。
2 实现 newInstance 方法来进行Fragment的 初始化和对argument的封装 为了向Fragment 插入数据
3 这里实现了 打电话和发短信的功能 并且传入了一个对象
public class CallMsgDialogFragment extends DialogFragment{
private CustomerMode cmode;
private int type;
public static CallMsgDialogFragment newInstance(CustomerMode cmode){
Bundle args = new Bundle();
args.putParcelable("customermode", cmode);
CallMsgDialogFragment fragment = new CallMsgDialogFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
cmode = getArguments().getParcelable("customermode");
return new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.notice))
.setMessage(getString(type==0?R.string.ensuresendmsg:R.string.ensurecall))
.setPositiveButton(getString(R.string.ensure), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(cmode!=null){
String phone = cmode.getPhone();
if(type==0){
sendMessage(phone);
}else{
call(phone);
}
}
}
})
.setNegativeButton(getString(R.string.cencle), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
}
private void sendMessage(String phone){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + phone));
getActivity().startActivity(intent);
}
private void call(String phone){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
getActivity().startActivity(intent);
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
具体实现类如下:
1 继承 DialogFragment 在onCreateDialog 方法中返回 一个Dialog 。
2 实现 newInstance 方法来进行Fragment的 初始化和对argument的封装 为了向Fragment 插入数据
3 这里实现了 打电话和发短信的功能 并且传入了一个对象
public class CallMsgDialogFragment extends DialogFragment{
private CustomerMode cmode;
private int type;
public static CallMsgDialogFragment newInstance(CustomerMode cmode){
Bundle args = new Bundle();
args.putParcelable("customermode", cmode);
CallMsgDialogFragment fragment = new CallMsgDialogFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
cmode = getArguments().getParcelable("customermode");
return new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.notice))
.setMessage(getString(type==0?R.string.ensuresendmsg:R.string.ensurecall))
.setPositiveButton(getString(R.string.ensure), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(cmode!=null){
String phone = cmode.getPhone();
if(type==0){
sendMessage(phone);
}else{
call(phone);
}
}
}
})
.setNegativeButton(getString(R.string.cencle), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
}
private void sendMessage(String phone){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + phone));
getActivity().startActivity(intent);
}
private void call(String phone){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
getActivity().startActivity(intent);
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
相关文章推荐
- Android 使用DialogFragment 实现一个可以多选的Dialog
- Android使用DialogFragment,自定义对话框
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android使用DialogFragment创建对话框
- Android 使用DialogFragment创建对话框
- Android开发丶使用DialogFragment实现炫酷的登录界面并实现DialogFragment与宿主Activity的通信
- android:使用DialogFragment 创建对话框
- Android中使用DialogFragment编写对话框的实例教程
- Android中使用DialogFragment 来创建对话框
- Android使用DialogFragment创建对话框
- Android中对话框(dialog)的使用
- Android开发中Dialog对话框的使用
- Android:创建常见对话框以及使用对话框实现登陆
- 【Android 开发】:UI控件之 TimePickerDialog 时间对话框的使用
- Android的DialogFragment的基本使用方法
- android下使用Fragment实现左侧3级菜单+动画效果
- android_使用ViewPager和Fragment实现滑动导航
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- 使用CXTPDialogBase简单实现resize对话框
- android DialogFragment使用 附例子