您的位置:首页 > 其它

AltertDialog在DialogFragment中的使用

2016-06-09 17:03 363 查看
从android3.0引入,若要支持android3.0以下需要使用support v4中的DialogFragment.

创建一个简单的Dialog

public class CustomDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.app_name)
.setPositiveButton("Sure",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}


和普通的创建AlertDialog,没有什么区别;

然后在需要调用的Activity中调用,调用方法如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomDialogFragment dialogFragment=new CustomDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "xxxx");
}




构建提醒对话框

Instantiate an AlertDialog.Builder with its constructor

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());


Chain together various setter methods to set the dialog characteristics

builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);


Get the AlertDialog from create()

AlertDialog dialog = builder.create();




4.添加“确定”等操作按钮

builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});




添加“取消”按钮

builder.setNegativeButton("Cancel", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});




5.在Dialog中展示列表

数据源

<string-array name="colors_array">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>


builder.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});




注意:setMessage后setItems将没有任何效果

这时点击某一项时Dialog就会消失,如果不想让其消失使用setSingleChoiceItems取代setItems.如下所示

Dialog单选模式

将setItems修改为setSingleChoiceItems

builder.setSingleChoiceItems(R.array.colors_array, 0,new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});


注意:setSingleChoiceItems第二个参数是默认选中的位置,如果没有请设置为-1。



如果想一次选中多个,请看下文,Dialog多选模式。

Dialog多选模式

如果想多选请使用setMultiChoiceItems方法,其用法如下:

builder
.setMultiChoiceItems(R.array.colors_array, new boolean[]{true,true,false},
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
});


与单选模式类似,如果不想默认选中,第二个参数可以传递null;

注意:在这里用于计数的数组mSelectedItems初始情况要与默认选中情况相同

自定义布局

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("Sign in", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}




Activity获取Dialog操作情况

public class CustomDialogFragment extends DialogFragment {
private DialogButtonListener dialogButtonListener;
public interface DialogButtonListener{
public void onPositiveClick(CustomDialogFragment customDialogFragment);
public void onNegativeClick(CustomDialogFragment customDialogFragment);

}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

if(!(activity instanceof DialogButtonListener)){
throw new RuntimeException("activity must implement DialogButtonListener ");
}else{
dialogButtonListener=(DialogButtonListener)activity;
}

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("XXXXXX");
builder.setPositiveButton("Sign in", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialogButtonListener.onPositiveClick(CustomDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialogButtonListener.onNegativeClick(CustomDialogFragment.this);
}
});
return builder.create();
}


public class MainActivity extends FragmentActivity implements DialogButtonListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomDialogFragment dialogFragment=new CustomDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "xxxx");
}
@Override
public void onPositiveClick(CustomDialogFragment customDialogFragment) {
Toast.makeText(this, "positive clicked!", Toast.LENGTH_LONG).show();

}
@Override
public void onNegativeClick(CustomDialogFragment customDialogFragment) {
Toast.makeText(this, "negative clicked!", Toast.LENGTH_LONG).show();

}


写的挺多,思路很简单

1.CustomDialogFrament定义一个接口,让Activity实现;

2.在onAttach,CustomDialogFrament拿到Activity的引用;

3.在进行相关操作的时候,调用Activity中实现的方法。

自适应为Dialog还是全屏

public class CustomDialogFragment extends DialogFragment {

/** The system calls this to get the DialogFragment's layout, regardless
of whether it's being displayed as a dialog or an embedded fragment. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.activity_main, container, false);
}

/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using
// onCreateView() is
// to modify any dialog characteristics. For example, the dialog
// includes a
// title by default, but your custom layout might not need it. So here
// you can
// remove the dialog title, but you must call the superclass to get the
// Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}

}


mIsLargeLayout,为你判定展示Dialog还是全屏的变量

public void showDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();

if (mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
newFragment.show(fragmentManager, "dialog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
}


转载地址

https://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  组件