android - 将 EditText 值从 Fragment 传递到 Activity

标签 android android-studio android-fragments

我在 Activity 中包含的 fragment 中有一个注册表单。当在 fragment 中单击提交按钮时,我需要在主 Activity 中实现一个函数。通过实现这一行:

android:onClick="signUp"

它直接进入 Activity 。但是我怎样才能得到 fragment 值呢?这很复杂,因为我的 fragment 没有想法,因为我在同一个地方切换不同的 fragment 。

我试过的:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_sign_up, container, false);
    email =(EditText) v.findViewById(R.id.emailEt);
    mListener.setEmail(email.getText().toString());
    pass1 = (EditText) v.findViewById(R.id.passEt1);
    mListener.setPass1(pass1.getText().toString());
    pass2 = (EditText) v.findViewById(R.id.passEt2);
    mListener.setPass2(pass2.getText().toString());
    pseudo = (EditText) v.findViewById(R.id.pseudoEt);
    mListener.setPseudo(pseudo.getText().toString());
    signup =(Button) v.findViewById(R.id.signUpB);

    return v;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onSignUpFragmentInteraction(Uri uri);
    public void setEmail(String email);
    public void setPass1(String pass1);
    public void setPass2(String pass2);
    public void setPseudo(String pseudo);
}

然后在我的 Activity 中:

@Override
public void setEmail(String email) {

}

@Override
public void setPass1(String pass1) {

}

@Override
public void setPass2(String pass2) {

}

@Override
public void setPseudo(String pseudo) {

}

@Override
public void onLoginFragmentInteraction(Uri uri) {

}

但我现在不知道该怎么办...

最佳答案

Fragment 应该始终是可重用的并且独立于其父 Activity。您的注册表单位于 Fragment 中,因此您可以读取 Fragment 实例中的所有值。在 Fragment 中而不是在 Activity 中定义 OnClickListener。您可以在 Fragment 中进行验证和数据处理,然后将所有值传递给 Fragment 的父 Activity。一个很好的方法是在 Fragment 中定义一个接口(interface)并在 Activity 中实现它。 See here .

接口(interface)将有一个类似bool signUp(String username, String email, String password) 的方法。要从您的 Fragment 调用此方法,您必须保留对转换到接口(interface)的父 Activity 的引用:

private OnFragmentInteractionListener mCallback;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

有了那个引用你就可以调用接口(interface)的方法了:

public void signUp(View v) {
    // get your values here
    mCallback.signUp(...);
}

如果您使用 Fragment 中的 onClick 定义扩充布局,请确保在 Fragment 中实现该方法。

关于android - 将 EditText 值从 Fragment 传递到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294819/

相关文章:

android - 绑定(bind) fbo 时是否需要每帧调用 glFramebufferTexture2D?

安卓界面设计

android - 如何在 Android 4 中从 ActionBar 隐藏单个选项卡

android - 如何获得 ScrollView 的唯一直系子节点?

android - 如何检测我的应用程序何时在 android 中被强行杀死?

android - 我如何知道我的应用程序是由 Google Assistant 打开的,而不是正常启动的

java - 如何在Android Studio中添加多个声音按钮?

Flutter 插件未安装错误;运行时 'flutter doctor'

android-studio - 构建工具版本android失败

android-studio - 如何修复布局抽屉导航事件中的 setOpenableLayout