/**
* @date 创建时间: 2017/11/22 22:25
* @author zh_legendd
* @Description 自定义的dialog
* @Email [email protected]
* @Version 1.0
*/
public class SureExitDialog extends Dialog {
private Button btn_cancel, btn_sure;
private SureListener mSureListener;
public interface SureListener {
void sureExit();
}
public void setSureListener(SureListener mSureListener){
this.mSureListener=mSureListener;
}
public SureExitDialog(Context context) {
super(context, R.style.PayDialog);
initWindow();
show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
btn_sure = (Button) findViewById(R.id.btn_sure);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSureListener.sureExit();
dismiss();
}
});
}
/**
* 初始化window参数
*/
private void initWindow() {
Window dialogWindow = getWindow();
dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialogWindow.setAttributes(lp);
}
}
/**
* @date 创建时间: 2017/11/22 0022 22:32
* @author zh_legendd
* @Description 承载Fragment的Activity
* @Email [email protected]
* @Version 1.0
*/
public class FDActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fd);
//替换fragment
initView();
}
public void initView(){
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
transaction.add(R.id.fl_context,TrFragment.newInstance());
transaction.commit();
// transaction.add()
}
}
/**
* @date 创建时间: 2017/11/22 0022 22:34
* @author zh_legendd
* @Description 镶嵌在Activity的Fragment
* @Email [email protected]
* @Version 1.0
*/
public class TrFragment extends Fragment {
private static final int REQUEST_DATE=0;
//fragment的TAG
private static final String DIALOG_DATE="DialogDate";
private Button bt_jump;
public static TrFragment newInstance(){
//fargment与Activity之间传递数据
// Bundle args =new Bundle();
// args.putString();
TrFragment trFragment=new TrFragment();
// trFragment.setArguments(args);
return trFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_tr, null);
bt_jump=(Button)inflate.findViewById(R.id.bt_jump);
bt_jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// MyToast.showText("点击了");
FragmentManager fragmentManager = getFragmentManager();
TestDialogFragment dialog = TestDialogFragment.newInstance("test");
dialog.setTargetFragment(TrFragment.this,REQUEST_DATE);
dialog.show(fragmentManager,DIALOG_DATE);
}
});
return inflate;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode!= Activity.RESULT_OK){
return;
}
if(requestCode==REQUEST_DATE){
MyToast.showText(data.getStringExtra(TestDialogFragment.EXTRA_STR));
}
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE,R.style.PayDialog);
}
/**
* @date 创建时间: 2017/11/22 0022 22:44
* @author zh_legendd
* @Description 在DialogFragment布局
* @Email [email protected]
* @Version 1.0
*/
public class TestDialogFragment extends DialogFragment {
private static final String ARG_TEST="test";
public static final String EXTRA_STR="extra_str";
private Button btn_cancel, btn_sure;
public static TestDialogFragment newInstance(String str){
Bundle bundle=new Bundle();
bundle.putString(ARG_TEST,str);
TestDialogFragment testDialogFragment=new TestDialogFragment();
testDialogFragment.setArguments(bundle);
return testDialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE,R.style.PayDialog);
}
@Override
public void onStart() {
super.onStart();
initWindow();
}
/**
* 初始化window参数
*/
private void initWindow() {
Window dialogWindow = getDialog().getWindow();
dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity= Gravity.CENTER;
dialogWindow.setAttributes(lp);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View inflate = inflater.inflate(R.layout.fragment_dialog, null);
btn_sure = (Button) inflate.findViewById(R.id.btn_sure);
btn_cancel = (Button) inflate.findViewById(R.id.btn_cancel);
btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendResult(Activity.RESULT_OK,"str");
}
});
return inflate;
}
private void sendResult(int resultCode,String str){
if(getTargetFragment()==null){
return;
}
Intent intent=new Intent();
intent.putExtra(EXTRA_STR,str);
getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
}
}
DialogFragment