我确实需要HW的帮助,我对Java不好,但是我需要通过该课程才能毕业。如何将文本从EditText(在FragmentOne中)发送到TextView(在FragmentTwo中)?在哪里粘贴/编写代码?
此外,如果您还可以帮助我将按钮(FragmentOne)的颜色“转移”到ImageView(FragmentTwo),我该怎么做?
我曾尝试观看教程和阅读答案,但我不知道该怎么办,对此我感到很吃力,我能得到的任何帮助将非常感激!
片段一EditText editText;按钮button2,button3;
片段二TextView textView;ImageView imageView2;
主要活动
public class MainActivity extends AppCompatActivity implements
FragmentOne.OnFragmentInteractionListener,
FragmentTwo.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
FragmentOne fragment = FragmentOne.newInstance("HOLA", 0);
fragmentTransaction.add(R.id.fragmentOne, fragment);
fragmentTransaction.commit();
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public void onFragmentInteraction(String message, int colorid) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
FragmentTwo fragment = FragmentTwo.newInstance(message, colorid);
fragmentTransaction.replace(R.id.fragmentTwo, fragment);
fragmentTransaction.commit();
}
}
FragmentOne
public class FragmentOne extends Fragment {
EditText editText;
Button buttonFragmentOne;
Button button;
Button button2;
private ImageView imageView;
private int colorid;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private int mParam2;
private OnFragmentInteractionListener mListener;
public FragmentOne() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentOne.
*
*
*/
// TODO: Rename and change types and number of parameters
public static FragmentOne newInstance(String param1, int param2) {
FragmentOne fragment = new FragmentOne();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putInt(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
private void changeColor(){
imageView.setColorFilter(ContextCompat.getColor(getContext(), colorid));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getInt(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false);
editText= rootView.findViewById(R.id.editText);
button = rootView.findViewById(R.id.button2);
imageView = rootView.findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
colorid = android.R.color.holo_red_dark;
changeColor();
}
});
button2 = rootView.findViewById(R.id.button3);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
colorid = android.R.color.holo_orange_dark;
changeColor();
}
});
buttonFragmentOne = rootView.findViewById(R.id.buttonFragmentOne);
buttonFragmentOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onButtonPressed(null);
}
});
// Inflate the layout for this fragment
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(editText.getText().toString(), colorid);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(String message, int colorid);
}
}
FragmentTwo
public class FragmentTwo extends Fragment {
TextView textView;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private int mParam2;
private OnFragmentInteractionListener mListener;
public FragmentTwo() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentTwo.
*/
public static FragmentTwo newInstance(String param1, int param2) {
FragmentTwo fragment = new FragmentTwo();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putInt(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getInt(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_two, container,
false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
没有看到您的代码,很难回答。如果颜色和文本不会改变,则应将其作为值存储在values / strings和values / colors文件夹中。
您可能想要做的就是根据用户的输入进行更新。您可以使用findViewById(...)
查找ID-在一个片段中,您需要使用getView()
View view = getView();
view.findViewById(R.id.textView).setText(view.findViewById(R.id.editText).getText());