1. 简单使用
/**
* 显示系统的对话框Fragment
* @author mazaiting
* @date 2018/3/1
*/
public class CustomDialogFragment extends DialogFragment implements DialogInterface.OnClickListener{
private static final String TAG = "CustomDialogFragment";
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity())
.setTitle("标题")
.setMessage("消息")
.setPositiveButton("确定", this)
.setNegativeButton("取消", this)
.setCancelable(false);
// 这里不能调用show方法
return builder.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(getActivity(), "确定", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
new CustomDialogFragment().show(getSupportFragmentManager(), "custom");
2. 自定义View的DialogFragment
public class ViewDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return inflater.inflate(R.layout.dialog, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10">
<requestFocus/>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Button"/>
</LinearLayout>
new ViewDialogFragment().show(getSupportFragmentManager(), "view");
3. 接口通信
/**
* 接口通信
* @author mazaiting
* @date 2018/3/1
*/
public interface DataCallback {
/**
* 获取数据
* @param data 数据
*/
void getData(String data);
}
/**
* 接口通信
*
* @author mazaiting
* @date 2018/3/1
*/
public class DataDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity())
.setTitle("标题")
.setMessage("消息")
.setPositiveButton("确定", this)
.setNegativeButton("取消", this)
.setCancelable(false);
return builder.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
DataCallback callback = (DataCallback) getActivity();
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
callback.getData("取消");
break;
case DialogInterface.BUTTON_POSITIVE:
callback.getData("确定");
break;
default:
break;
}
}
}
public class MainActivity extends AppCompatActivity implements DataCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@OnClick({R.id.btn_custom, R.id.btn_view, R.id.btn_get_data})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_custom:
new CustomDialogFragment().show(getSupportFragmentManager(), "custom");
break;
case R.id.btn_view:
new ViewDialogFragment().show(getSupportFragmentManager(), "view");
break;
case R.id.btn_get_data:
new DataDialogFragment().show(getSupportFragmentManager(), "data");
break;
default:
break;
}
}
@Override
public void getData(String data) {
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
}