我是 Android 开发新手。我试图将值从片段传递到活动,但经过多次尝试后我无法得到答案......
这是我的片段:
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 类中实现该接口。
例如,在您的 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);
你的活动应该是这样的:
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
}
}
欲了解更多信息,您可以查看官方文档 https://developer.android.com/training/basics/fragments/communicating.html#Implement :
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)