如何将值从片段传递到活动 [英] How to pass values from Fragment to Activity
本文介绍了如何将值从片段传递到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Android开发的新手.我正在尝试将值从片段传递到活动,但是经过多次尝试,我无法获得答案...
I am new in Android Development. I am trying to pass values from fragment to activity, but after so many tries I am unable to get the answer...
这是我的片段:
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;
}
这是我要传递值的类
推荐答案
您应该在您的片段中创建一个接口,并且应该为Activity类实现该接口.
You should create an interface in your fragment and you should implement the interface to your Activity class.
例如,在您的片段中:
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");
}
}
您可以发送任何这样的值:
you can send any value like this:
// Send the event to the host activity
mCallback.onArticleSelected(position);
您的活动应如下所示:
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
}
}
有关更多信息,您可以查看官方文档:
For more information you can check the offical documentation :
这篇关于如何将值从片段传递到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文