android - 如何将值从 Fragment 传递到 Activity

标签 android android-fragments

我是 Android 开发的新手。我正在尝试将值从 fragment 传递到 Activity ,但经过多次尝试后我无法得到答案......

这是我的 fragment :

public OtpGentation(int OTP)
{
    this.OTP =OTP;
}
public OtpGentation(String number1, String email1, String password)
{
    number = number1;
    mail = email1;
    pass = password;
}

public OtpGentation() {


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    rootview = inflater.inflate(R.layout.otp, container, false);
    Bundle bundle = getArguments();

    new OTPAsyncManager().execute();

    etxotp =(EditText)rootview.findViewById(R.id.etxotp);
    btnNext = (Button) rootview.findViewById(R.id.nextToOTP);
    btnCancel =(Button) rootview.findViewById(R.id.cancel);

    //etxotp.setText(OTP);
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {

            if (etxotp.getText().toString().equals(""))
            {
                Toast.makeText(getContext(), "Please Enter OTP ", Toast.LENGTH_SHORT).show();

            }
            else
            {
                int enteredOtp = Integer.parseInt(etxotp.getText().toString());
                if (enteredOtp ==OTP)
                {

                    Toast.makeText(getContext(), "OTP Matched", Toast.LENGTH_SHORT).show();
                    Bundle bun = new Bundle();
                    bun.putString("no",number);
                    bun.putString("ma",mail);
                    bun.putString("pa",pass);
                    Intent subintent = new Intent(getContext(),SubmitRegistration.class);
                    subintent.putExtras(bun);
                    startActivity(subintent);



                }
                else
                {
                    Toast.makeText(getContext(), "Please Enter Correct OTP ", Toast.LENGTH_SHORT).show();
                }

            }


        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {

            fragmentManager = getActivity().getSupportFragmentManager();
            HomeScreen fragmentOne = new HomeScreen();

            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                    .replace(R.id.content_frame, fragmentOne)
                    .addToBackStack("")
                    .commit();

        }
    });


    return rootview;
}

这是我要传递值的类

最佳答案

您应该在您的 fragment 中创建一个接口(interface),并且您应该为您的 Activity 类实现该接口(interface)。

例如,在您的 fragment 中:

OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

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

您可以像这样发送任何值:

        // Send the event to the host activity
        mCallback.onArticleSelected(position);

你的 Activity 应该是这样的:

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

更多信息您可以查看the offical documentation :

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

相关文章:

java - fragment 、AsyncTask 和监听器

android - 谷歌地图发布 key 的 SHA1 显示一些加密文本

android - 如何在带有 Koltllin 的 Android 应用程序中使用 Response.Listener Volley

android - 无法用新 fragment 替换 fragment

android - android 中的 fragment 事务导致黑屏

android - Fragments 与弃用的 TabActivity 在 3.0 之前的设备上

android - 使用 Dispatchers.IO 进行 Kotlin 协程测试

Android 构建 gradle - 替换字符串资源值

Android:以编程方式设置 View 样式

java - 如何在 Fragment 中使用 map