如何将片段中的值传递给活动
我是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;
}
而这正是我想要的类通过数值
这是我使用的解决方案,这工作,但它不是一个好的做法
内,您的活动写getter和setter方法要传递的数据,例如,如果你想传递一个字符串名称写
String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
在片段
public class YourFragment extends Fragment {
Context contextCheckClass;
private String fileName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View group=(View)inflater.inflate(R.layout.fragment_highlight, null);
fileName=((MainActivity) getActivity()).setFileName("Your String to pass to activity"); //set what ever string you want to pass
return group;
}
}
如果你这样做的方式,你不能在其他任何地方使用这个片段,如果要访问不同的活动相同片断,然后
MainActivity
在你的片段中创建一个构造函数传递上下文。你会注意到它会给出一个关于避免非默认构造函数忽略它或禁止检查的警告
public class YourFragment extends Fragment {
private String fileName;
Context contextCheckClass;
public YourFragment (Context ctx) {
this.contextCheckClass=ctx;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View group=(View)inflater.inflate(R.layout.fragment_highlight, null);
if(contextCheckClass instanceof MainActivity){ //write your Activity name here
//This fragment is called from MainActivity
fileName=((MainActivity) getActivity()).getFileName();
}
else {
//This fragment is called from some other activity
}
return group;
}
加载该片段通过活动情境它
YourFragment yourFragment =new YourFragment (MainActivity.this);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, yourFragment)
.addToBackStack(null)
.commit();
你应该在你的片段创建一个接口,你应该实现的接口到你的Activity类。
例如,在您的片段:
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
}
}
更多您可以检查的信息the offical documentation:
onAttach(活动活动):现在不推荐使用。 –
是的,onAttach(上下文上下文)更有意义。 – ziLk
它看起来像你的问题是不完整 – Manza
是代码给你一个错误,如果是的话显示我们 –