在实际编程过程中,我们经常碰到这样一种情况:
在新建的Dialog
或者Fragment
中修改数据,但是要在Activity
或其他位置进行数据更新,那怎么处理呢?此时我们的回调机制就派上了用场。
那么什么是回调机制呢,回调机制是一种依赖于接口的传递机制,旨在为其他类暴漏方法以使得其他类具备获取数据权限(PS:这里的权限指的是修饰词引起的访问权限)。说这么多,估计大多数人还是一头雾水,接下来,让我们写一个Demo实践一下吧。
我们在新建的项目中新建CustomDialogFragment
类,继承自DialogFragment
并重写基本构造方法,代码如下:
public class CustomDialogFragment extends DialogFragment {
public CustomDialogFragment() {
super();
}
}
接着创建CustomDialogFragment
的布局文件,并重写其onCreateView()
方法加载自定义布局。
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name"
android:scaleType="center"
/>
<EditText
android:id="@+id/id_txt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="input username"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/id_txt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:hint="input password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="@+id/id_cancel"
android:gravity="center"
android:text="CANCEL"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<View
android:background="@android:color/darker_gray"
android:layout_width="2dp"
android:layout_height="match_parent">View>
<TextView
android:gravity="center"
android:id="@+id/id_ok"
android:text="OK"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
LinearLayout>
LinearLayout>
修改后的CustomDialogFragment
代码如下:
public class CustomDialogFragment extends DialogFragment {
private View mRootView;
private EditText mUserNameEditText,mPassWordEditText;
private TextView mOkTextView,mCancelTextView;
public CustomDialogFragment() {
super();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.custom_fragment_layout,null);
initView();
initData();
return mRootView;
}
public void initView(){
mUserNameEditText = (EditText)mRootView.findViewById(R.id.id_txt_username);
mPassWordEditText = (EditText)mRootView.findViewById(R.id.id_txt_password);
mOkTextView = (TextView)mRootView.findViewById(R.id.id_ok);
mCancelTextView = (TextView)mRootView.findViewById(R.id.id_cancel);
}
public void initData(){
}
}
如上所示,我们基本完成了CustomDialogFragment中View的初始化,接下来就是为View绑定事件,将两个EditText中的内容回传到Activity中.
新建OnLoginInforCompleted
接口,其内部包含一个输入用户信息完成后的方法,如下所示:
public interface OnLoginInforCompleted {
void inputLoginInforCompleted(String userName,String passWord);
}
在CustomDialogFragment
中新建该接口类型的私有成员并,生成get方法,并在Ok点击事件中回调该接口,传入用户输入数据,如下所示:
public class CustomDialogFragment extends DialogFragment {
private View mRootView;
private EditText mUserNameEditText,mPassWordEditText;
private TextView mOkTextView,mCancelTextView;
private OnLoginInforCompleted mOnLoginInforCompleted;
public void setOnLoginInforCompleted(OnLoginInforCompleted onLoginInforCompleted) {
mOnLoginInforCompleted = onLoginInforCompleted;
}
public CustomDialogFragment() {
super();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.custom_fragment_layout,null);
initView();
initData();
return mRootView;
}
public void initView(){
mUserNameEditText = (EditText)mRootView.findViewById(R.id.id_txt_username);
mPassWordEditText = (EditText)mRootView.findViewById(R.id.id_txt_password);
mOkTextView = (TextView)mRootView.findViewById(R.id.id_ok);
mCancelTextView = (TextView)mRootView.findViewById(R.id.id_cancel);
}
public void initData(){
mOkTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnLoginInforCompleted.inputLoginInforCompleted(mUserNameEditText.getText().toString(),mPassWordEditText.getText().toString());
dismiss();
}
});
mCancelTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
接下来我们完成Activity
部分的编码,首先在Activity
的布局中添加用于展示用户输入信息的TextView
,并使Activity实现OnLoginInforCompleted
,在inputLoginInforCompleted(userName,passWord)
方法中为TextView设置文本,具体代码如下所示:
public class MainActivity extends AppCompatActivity implements OnLoginInforCompleted{
private TextView mContentTextView;
private CustomDialogFragment mCustomDialogFragment;
public static final String COMMAND = "USER_INFOR_DIALOG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContentTextView = (TextView) findViewById(R.id.user_input_content);
mCustomDialogFragment = new CustomDialogFragment();
mCustomDialogFragment.setOnLoginInforCompleted(this);
mContentTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCustomDialogFragment.show(getFragmentManager(),COMMAND);
}
});
}
@Override
public void inputLoginInforCompleted(String userName, String passWord) {
mContentTextView.setText(userName+"::::"+passWord);
}
}
至此,我们就完成了一个简单回调Demo的编码,源码戳这里:
源码
通过上述代码,我们可以看出核心的数据流向是下图这样的:
最新文章更新在微信公众号上,欢迎关注获取详情: