在上篇文档中,我主要记录了通过接口回调实现了DialogFragment与宿主Acitivity的通信,但是在实际开发过程中,登录界面往往是从“我的”这个Fragment打开的,这里发现接口回调不是很好用,查阅了很多文档,都没有很好的解决办法,因此,怎样把输入的账户名和密码由DialogFragment传递给宿主Fragment以便完成进一步的网络请求等操作就成了我们亟待解决的问题,这里我们通过setTartgetFragment()这个方法即可解决。
这里因为界面等基本与上篇与Activity交互的文档相同,所以不再详细赘述,可以参考如下文档。
http://blog.csdn.net/u014078990/article/details/79286850
首先看下实际效果。
实现步骤:
1.在DialogFragment这个方法里,在登录按钮的onClick点击事件里设置监听。
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login_btn:
if (getTargetFragment()== null){
return;
}
Intent intent= new Intent();
intent.putExtra(USERNAME, mUsername.getText().toString());
intent.putExtra(USERPASSWORD, mPassword.getText().toString());
getTargetFragment().onActivityResult(MainFragment.REQUEST_CODE, Activity.RESULT_OK, intent);
break;
}
}
完整代码:
package com.fantasychong.dialogfragmentlogin2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* Created by lenovo on 2018/2/8.
*/
public class LoginDailogFragment extends DialogFragment implements View.OnClickListener {
public static final String USERNAME = "userName";
public static final String USERPASSWORD = "userPassword";
private EditText mUsername;
private EditText mPassword;
private Button btn;
private ImageView iv;
private TextView toReg;
private ProgressBar pb;
private LoginDailogFragment fragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//设置背景透明
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return super.onCreateView(inflater, container, savedInstanceState);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view= LayoutInflater.from(getActivity()).inflate(R.layout.dialog_login, null);
iv= view.findViewById(R.id.login_iv);
toReg= view.findViewById(R.id.login_register);
mUsername= view.findViewById(R.id.login_et1);
btn= view.findViewById(R.id.login_btn);
mPassword= view.findViewById(R.id.login_et2);
iv.setOnClickListener(this);
toReg.setOnClickListener(this);
btn.setOnClickListener(this);
builder.setView(view);
return builder.create();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login_btn:
if (getTargetFragment()== null){
return;
}
Intent intent= new Intent();
intent.putExtra(USERNAME, mUsername.getText().toString());
intent.putExtra(USERPASSWORD, mPassword.getText().toString());
getTargetFragment().onActivityResult(MainFragment.REQUEST_CODE, Activity.RESULT_OK, intent);
break;
}
}
}
2.在宿主Fragment里设置onActivityResult方法设置回调的参数。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== REQUEST_CODE){
String userName= data.getStringExtra(LoginDailogFragment.USERNAME);
String userPassword= data.getStringExtra(LoginDailogFragment.USERPASSWORD);
name.setText(userName);
password.setText(userPassword);
fragment.dismiss();
}
}
3.在宿主Fragment打开DialogFragment的入口处设置监听。
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment= new LoginDailogFragment();
fragment.setTargetFragment(MainFragment.this, REQUEST_CODE);
fragment.show(getFragmentManager(), "login");
}
});
完整代码:
package com.fantasychong.dialogfragmentlogin2;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainFragment extends Fragment {
public static final int REQUEST_CODE = 1;
private Button login;
private TextView name;
private TextView password;
private LoginDailogFragment fragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= LayoutInflater.from(getActivity()).inflate(R.layout.fragment_main, container, false);
login= view.findViewById(R.id.main_login);
name= view.findViewById(R.id.main_name);
password= view.findViewById(R.id.main_password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment= new LoginDailogFragment();
fragment.setTargetFragment(MainFragment.this, REQUEST_CODE);
fragment.show(getFragmentManager(), "login");
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== REQUEST_CODE){
String userName= data.getStringExtra(LoginDailogFragment.USERNAME);
String userPassword= data.getStringExtra(LoginDailogFragment.USERPASSWORD);
name.setText(userName);
password.setText(userPassword);
fragment.dismiss();
}
}
}
至此全部完成,我们可以看到无论是宿主Activity还是宿主Fragment都可以接收到DialogFragment回调过来的参数,我们根据实际开发需求去选取即可。
源码下载:点击打开链接