本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/我们也可以创建另外一种碎片——DialogFragment。顾名思义,DialogFragment就是一个浮动在Activity上面的Fragment。当需要用户的反馈时,DialogFragment就会派上用场。与使用ListFragment类似,需要继承DialogFragment基类。
下面将会展示如何使用DialogFragment。
1. 创建一个工程:DialogFragmentExample。
2. 在包路径下面新建一个类,Fragment1。
public class Fragment1 extends DialogFragment { static Fragment1 newInstance(String title) { Fragment1 fragment = new Fragment1(); Bundle args = new Bundle(); args.putString("title", title); fragment.setArguments(args); return fragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String title = getArguments().getString("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_launcher) .setTitle(title) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doPositiveClick(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doNegativeClick(); } }).create(); } }3. DialogFragmentExampleActivity.java中的代码。
public class DialogFragmentExampleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Fragment1 dialogFragment = Fragment1.newInstance( "Are you sure you want to do this?"); dialogFragment.show(getFragmentManager(), "dialog"); } public void doPositiveClick() { //---perform steps when user clicks on OK--- Log.d("DialogFragmentExample", "User clicks on OK"); } public void doNegativeClick() { //---perform steps when user clicks on Cancel--- Log.d("DialogFragmentExample", "User clicks on Cancel"); } }4. 按F11在模拟器上调试。会看到一个对话框,点击OK或Cancel按钮会弹出消息。
要创建一个DialogFragment,首先要继承DialogFragment基类:
public class Fragment1 extends DialogFragment{ }在这个例子中,我没创建一个警告对话框,它包含一条警告消息和两个可供点击的按钮。在Fragment1类中,我们定义了一个newInstance()方法:
static Fragment1 newInstance(String title) { Fragment1 fragment = new Fragment1(); Bundle args = new Bundle(); args.putString("title", title); fragment.setArguments(args); return fragment; }这个newInstance()方法允许创建一个碎片的实例对象,同时,它也可以接受一个指定的字符串参数,这个参数就是对话框中的消息。title对象被存储在一个Bundle对象中,之后会用到它。
接下来,我们定义了onCreateDialog()方法,这个方法在onCreate()之后,onCreateView()之前被调用:
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { String title = getArguments().getString("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_launcher) .setTitle(title) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doPositiveClick(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((DialogFragmentExampleActivity) getActivity()).doNegativeClick(); } }).create(); }然后,我们可以创建两个按钮OK和Cancel。对话框的标题是从Bunddle对象中取出来的。
想要把对话框碎片显示出来,我们创建一个它的实例,然后调用show()方法即可:
Fragment1 dialogFragment = Fragment1.newInstance( "Are you sure you want to do this?"); dialogFragment.show(getFragmentManager(), "dialog");同时,我们需要实现两个回调方法,doPositiveClick()和doNegativeClick(),通过这两个方法去处理用户的输入事件:
public void doPositiveClick() { //---perform steps when user clicks on OK--- Log.d("DialogFragmentExample", "User clicks on OK"); } public void doNegativeClick() { //---perform steps when user clicks on Cancel--- Log.d("DialogFragmentExample", "User clicks on Cancel"); }
注:在Android 4.0版本以后,官方推荐使用DialogFragment去替换Dialog。大家可以看看Android 4.0的Launcher等源码,这些源码中大量使用了Fragment类。