1、DialogFragment的优势及用处
DialogFragment是一种特殊的Fragment一般用于展示确认框、警告框、对话框、输入框等等,
与AlertDialog以及Dialog相比,DialogFragment在旋转屏幕以及按下退后键时能更好的管理其生命周期,
2、DialogFragment的用法
使用DialogFragment至少需要实现onCreateView或者onCreateDialog方法,onCreateView为选用自定义的XML布局文件展示Dialog,onCreateDialog则利用AlertDialog或者Dialog创建出Dialog,
3、重写onCreateView示例代码
a)布局文件(重写onCreateView)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#00c5cd"
>
<EditText
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:textSize="30sp"
android:hint="input your name" />
</RelativeLayout>
<Button
android:id="@+id/sure_button"
android:layout_below="@id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:background="#EEB422"
android:text="确定"/>
<Button
android:id="@+id/cancel_button"
android:layout_below="@id/sure_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:text="取消"
android:background="#EE0000"
/>
</RelativeLayout>
b)继承DialogFragment,重写onCreateView方法
package com.example.everythingtest;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
public class Show_DialogFragment extends DialogFragment implements OnClickListener{
private TextView sureText;
private TextView cancelText;
SureQuitListener mSureQuitListener;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题栏
View view = inflater.inflate(R.layout.dialog_fragment, container);
sureText = (TextView) view.findViewById(R.id.sure_button);
cancelText = (TextView) view.findViewById(R.id.cancel_button);
sureText.setOnClickListener(this);
cancelText.setOnClickListener(this);
return view;
}
public void setSureQuitListener (SureQuitListener sureQuitListener) {
this.mSureQuitListener = sureQuitListener;
}
@Override
public void onResume() {
getDialog().setCanceledOnTouchOutside(false);
super.onResume();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sure_button:
mSureQuitListener.sure(getActivity());
break;
case R.id.cancel_button:
dismiss();
break;
default:
break;
}
}
interface SureQuitListener {
public void sure(Context context);
}
}
c) 布局文件(重写onCreateDialog方法)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="6dp"
android:paddingBottom="6dp" >
<EditText
android:id="@+id/name_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"/>
<EditText
android:id="@+id/password_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:layout_below="@id/name_content"
android:hint="password"/>
<!-- <Button
android:id="@+id/confire_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_below="@id/password_content"
android:text="确定"
android:background="#EE0000"
/>-->
</RelativeLayout>
package com.example.everythingtest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class Show_AlertDialog extends DialogFragment {
private EditText nameContent;
private EditText passWordContent;
LoginListener mLoginListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//get the layoutInflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.alert_dialog_fragment, null);
nameContent = (EditText) view.findViewById(R.id.name_content);
passWordContent = (EditText) view.findViewById(R.id.password_content);
//inflate and set the layout for the dialog
builder.setView(view)
//add action buttons
.setPositiveButton("sign in", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mLoginListener.getInput(nameContent.getText().toString(),
passWordContent.getText().toString());
}
}).setNegativeButton("cancel", null);
return builder.create();
}
interface LoginListener{
public void getInput(String name, String password);
}
public void setLoginListener(LoginListener listener) {
this.mLoginListener = listener;
}
}
e)测试运行,Main中调用
package com.example.everythingtest;
import com.example.everythingtest.Show_AlertDialog.LoginListener;
import com.example.everythingtest.Show_DialogFragment.SureQuitListener;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements SureQuitListener, LoginListener {
private Button showDialogFragmentButton;
private Button showAlertDialogButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("zcc", "excute onCreate");
}
@Override
protected void onResume() {
showDialogFragmentButton = (Button) findViewById(R.id.dialog_fragment);
showAlertDialogButton = (Button) findViewById(R.id.alert_dialog_fragment);
showDialogFragmentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_fragment:
showEditDialog(v);
break;
default:
break;
}
}
});
showAlertDialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alert_dialog_fragment:
showAlertDialog();
break;
default:
break;
}
}
});
super.onResume();
}
/**
* 调用重写onCreateView的DialogFragment
* @param view
*/
public void showEditDialog(View view){
Show_DialogFragment mShow_DialogFragment = new Show_DialogFragment();
mShow_DialogFragment.show(getFragmentManager(), "1111");
mShow_DialogFragment.setSureQuitListener(this);
}
/**
* 调用重写onCreateDialog的DialogFragment
*/
public void showAlertDialog() {
Show_AlertDialog mAlertDialog = new Show_AlertDialog();
mAlertDialog.show(getFragmentManager(), "2222");
mAlertDialog.setLoginListener(this);
}
@Override
public void sure(Context context) {
finish();
}
@Override
public void getInput(String name, String password) {
Toast.makeText(this, "name =" + name + ", password =" +password, Toast.LENGTH_LONG).show();
}
}