繁体   English   中英

如何从片段外部获取片段内部的编辑文本值

[英]How to get a edit text value inside a fragment from outside of the fragment

我试图与点击fragment_admin_upload_product.xml 图像上传来获得产品名称上的片段(addProductFragment.xml) 但这使活动崩溃,我意识到我无法从框架布局的外部直接获取片段内部的值。

GUI导致问题

fragment_admin_upload_product.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/FrameLayout2" android:layout_margin="30dp" android:layout_width="409dp" android:layout_height="729dp" tools:layout_editor_absoluteX="1dp" tools:layout_editor_absoluteY="1dp" tools:ignore="MissingConstraints" > </FrameLayout> <ImageView android:layout_marginEnd="40dp" android:layout_marginBottom="40dp" android:id="@+id/add_product_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/add_icon" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:background="#e3d4bf"/> <ImageView android:layout_marginEnd="40dp" android:layout_marginBottom="40dp" android:id="@+id/upload_product_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/upload_icon" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:visibility="gone" android:background="#e3d4bf" /> </RelativeLayout> 

UploadProductFragment.java

 package com.example.imlproject.ui.admin_upload_product; import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import com.example.imlproject.R; import com.example.imlproject.addProductFragment; import com.example.imlproject.addedProductListFragment; import com.google.firebase.storage.internal.Util; import java.util.regex.Pattern; public class UploadProductFragment extends Fragment { private UploadProductViewModel uploadProductViewModel; private ImageView addImageView; private ImageView uploadImageView; //EditText productName,productDescription,productPrice,productStock; public View onCreateView ( @NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) { uploadProductViewModel = ViewModelProviders.of ( this ).get ( UploadProductViewModel.class ); View root = inflater.inflate ( R.layout.fragment_admin_upload_product , container , false ); FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment newFragment = new addedProductListFragment(); fragmentTransaction.replace(R.id.FrameLayout2,newFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); //Spinner typeProductSelection= (Spinner)root.findViewById(R.id.productTypeSpinner); //productName = (EditText)root.getRootView().findViewById(R.id.productNameEditText); //productDescription = (EditText)root.findViewById(R.id.productNameEditText); //productPrice = (EditText)root.findViewById(R.id.productNameEditText); //productStock = (EditText)root.findViewById(R.id.productNameEditText); // Pattern regexName= Pattern.compile ( "^[a-zA-Z0-9 ]{3,}$" ); addImageView = root.findViewById(R.id.add_product_icon); uploadImageView = root.findViewById(R.id.upload_product_icon); addImageView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); Fragment newFragment = new addProductFragment(); fragmentTransaction.replace(R.id.FrameLayout2,newFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); addImageView.setVisibility(View.GONE); uploadImageView.setVisibility(View.VISIBLE); } }); uploadImageView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // String productNameStr = productName.getText().toString(); //String productDescriptionStr= productDescription.getText().toString(); //String productPriceStr = productPrice.getText().toString(); //String productStockStr = productPrice.getText().toString(); /* if( TextUtils.isEmpty(productNameStr) ) { //Toast.makeText(getActivity(),"Something wrong",Toast.LENGTH_LONG).show(); } else {*/ FragmentManager fragmentManager =getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); Fragment newFragment =new addedProductListFragment(); fragmentTransaction.replace(R.id.FrameLayout2,newFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); uploadImageView.setVisibility(View.GONE); addImageView.setVisibility(View.VISIBLE); // } } }); return root; } } 

fragment_add_product.xml

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".addProductFragment"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/AddProductFragmentRelativeLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linearLayout21" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/add_icon" /> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:id="@+id/linearLayout27" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/productTypeTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Type of product" android:textColor="#000000" android:textSize="15dp"/> <Spinner android:id="@+id/productTypeSpinner" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:id="@+id/linearLayout23" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/productNameTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Product Name" android:textColor="#000000" android:textSize="15dp"/> <EditText android:id="@+id/productNameEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:inputType="textPersonName" android:hint="Enter a product name" android:textColor="#000000" android:textSize="15dp"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:id="@+id/linearLayout24" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/productDescriptionTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Description" android:textColor="#000000" android:textSize="15dp"/> <EditText android:id="@+id/productDescriptionEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:inputType="textPersonName" android:hint="Enter a description" android:textColor="#000000" android:textSize="15dp"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:id="@+id/linearLayout25" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/productPriceTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Price" android:textColor="#000000" android:textSize="15dp"/> <EditText android:id="@+id/productPriceEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:inputType="textPersonName" android:hint="Enter a price" android:textColor="#000000" android:textSize="15dp"/> </LinearLayout> <LinearLayout android:layout_marginTop="10dp" android:id="@+id/linearLayout26" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/productQuantityLeftTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Quantity Left" android:textColor="#000000" android:textSize="15dp"/> <EditText android:id="@+id/productQuantityLeftEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:inputType="textPersonName" android:hint="Enter number of stock" android:textColor="#000000" android:textSize="15dp"/> </LinearLayout> </LinearLayout> </RelativeLayout> </ScrollView> </FrameLayout> 

addProductFragment.java

 package com.example.imlproject; import android.content.Context; import android.net.Uri; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import java.util.regex.Pattern; public class addProductFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ View v = inflater.inflate(R.layout.fragment_add_product,container,false); Spinner typeProductSelection = (Spinner)v.findViewById(R.id.productTypeSpinner); ArrayAdapter<String> typeProductAdapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.productType)); typeProductAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); typeProductSelection.setAdapter(typeProductAdapter); return v; } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM