将片段内的值发送到我的活动

时间:2017-03-11 12:39:56

标签: android android-fragments

这是我的主要活动:

public class UploadApp extends AppCompatActivity  implements StepperLayout.StepperListener{

private StepperLayout mStepperLayout;
Toolbar uploadAppToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_app);

    uploadAppToolbar = (Toolbar) findViewById(R.id.uploadAppToolbar);
    uploadAppToolbar.setTitle("Application Upload");

    mStepperLayout = (StepperLayout) findViewById(R.id.stepperLayout);
    mStepperLayout.setAdapter(new UploadAppStepperAdapter(getSupportFragmentManager(), this));

}}

步进适配器:

public class UploadAppStepperAdapter extends AbstractFragmentStepAdapter {


public UploadAppStepperAdapter(@NonNull FragmentManager fm, @NonNull Context context) {
    super(fm, context);
}

@Override
public Step createStep(@IntRange(from = 0L) int position) {
    switch (position) {
        case 0:
            return AppUploadStep1.newInstance(R.layout.content_app_upload_step1);
        case 1:
            return AppUploadStep2.newInstance(R.layout.content_app_upload_step2);
        case 2:
            return AppUploadStep3.newInstance(R.layout.content_app_upload_step3);
        case 3:
            return AppUploadStep4.newInstance(R.layout.content_app_upload_step4);
        default:
            throw new IllegalArgumentException("Unsupported position: " + position);
    }
}

@Override
public int getCount() {
    return 4;
}

@NonNull
@Override
public StepViewModel getViewModel(@IntRange(from = 0) int position) {
    //Override this method to set Step title for the Tabs, not necessary for other stepper types
        StepViewModel.Builder builder = new StepViewModel.Builder(context);

    switch (position) {
        case 0:
            builder
                      .setTitle("Information"); //can be a CharSequence instead
            break;
        case 1:
            builder
                    .setTitle("Logo");
            break;
        case 2:
            builder
                    .setTitle("Sample Images");
            break;
        case 3:
            builder
                    .setTitle("Application Data");
            break;
        default:
            throw new IllegalArgumentException("Unsupported position: " + position);
    }
    return builder.create();
}

}

我的一个片段(AppUploadStep1):

public class AppUploadStep1 extends Fragment implements BlockingStep {

private static final String LAYOUT_RESOURCE_ID_ARG_KEY = "messageResourceId";

EditText edtAppName, edtAppVersion, edtAppPlatform, edtAppCategory, edtAppDescription;
TextInputLayout  tilAppName,  tilAppVersion,  tilAppPlatform,  tilAppCategory,  tilAppDescription;
String sAppName, sAppVersion, sAppPlatform, sAppCategory, sAppDescription;
boolean f1, f2, f3, f4, f5;

HttpURLConnection connection;
BufferedReader reader;
URL url;
InputStream stream;
StringBuffer buffer;
String line;

ProgressBar loading;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.content_app_upload_step1, container, false);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    //initialize your UI
    edtAppPlatform = (EditText) v.findViewById(R.id.edtAppPlatform);
    edtAppVersion = (EditText) v.findViewById(R.id.edtAppVersion);
    edtAppCategory = (EditText) v.findViewById(R.id.edtAppCategory);
    edtAppDescription = (EditText) v.findViewById(R.id.edtAppDescription);
    edtAppName = (EditText) v.findViewById(R.id.edtAppName);

    tilAppName = (TextInputLayout) v.findViewById(R.id.tilAppName);
    tilAppVersion = (TextInputLayout) v.findViewById(R.id.tilAppVersion);
    tilAppPlatform = (TextInputLayout) v.findViewById(R.id.tilAppPlatform);
    tilAppCategory = (TextInputLayout) v.findViewById(R.id.tilAppCategory);
    tilAppDescription = (TextInputLayout) v.findViewById(R.id.tilAppDescription);

    edtAppPlatform.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showCustomSpinnerDialog(v, R.array.spinner_platform, R.id.edtAppPlatform);
        }
    });

    edtAppCategory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showCustomSpinnerDialog(v, R.array.categories, R.id.edtAppCategory);
        }
    });

    errorTrap();
    return v;
}

public static AppUploadStep1 newInstance(@LayoutRes int layoutResId) {
    Bundle args = new Bundle();
    args.putInt(LAYOUT_RESOURCE_ID_ARG_KEY, layoutResId);
    AppUploadStep1 fragment = new AppUploadStep1();
    fragment.setArguments(args);
    return fragment;
}

我的问题是,如何在我的Activity上的Fragment AppUploadStep1中的所有edittexts中输入值?我正在使用github的stepper库创建一个注册表单。

1 个答案:

答案 0 :(得分:0)

要在Fragment和Activity之间传递数据,你必须使用类似这样的东西

发送

Bundle args = new Bundle();
                args.putString("id", id);
                args.putString("title",title);
                args.putString("desc",des);
                editDialog.setArguments(args);

保留

  String title = getArguments().getString("title");
    String des = getArguments().getString("desc");