我有两个片段,我想在我的屏幕上逐一显示它们。它们由mainactivity管理。第一个片段包含一个textview和一个按钮。第二个片段包含一个edittext和一个按钮。我试图做的是:第一个片段显示在屏幕上,当用户按下按钮时,我希望我的活动显示第二个片段,当用户按下此片段中的按钮时,我希望将文本从edittext发送到第一个片段,并在textview上显示。我为听众使用了这个界面。
public class MainActivity extends AppCompatActivity implements Listener, ListenerTwo{
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
}
@Override
public void onButtonSelected() {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentTwo());
fragmentTransaction.commit();
}
@Override
public void onButtonPressed(String text) {
FragmentOne fragmentOne = (FragmentOne)getSupportFragmentManager().findFragmentById(R.id.fragmentPlaceholder);
if (fragmentOne != null)
fragmentOne.setTextt(text);
}
}
在我的片段中:
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button);
textView = view.findViewById(R.id.textView);
Listener listener = (Listener) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonSelected();
}
});
}
public void setTextt (String text) {
textView.setText(text);
}
第二部分:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = view.findViewById(R.id.button2);
EditText editText = view.findViewById(R.id.editText);
ListenerTwo listener = (ListenerTwo) getActivity();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "button clicked", Toast.LENGTH_SHORT).show();
if (listener != null)
listener.onButtonPressed(editText.getText().toString());
}
});
}
1条答案
按热度按时间vx6bjr1n1#
在…内
Activity
在里面onButtonPressed
当前设置的正在强制转换的方法FragmentTwo
到FragmentOne
. 请注意,当您设置Fragment
您正在使用fragmentTransaction.replace(
<-更换!您正在将片段加载到R.id.fragmentPlaceholder
,当设置新的时,将销毁以前的在…内
onButtonPressed
你必须重新设置FragmentOne
```fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentPlaceholder, new FragmentOne());
fragmentTransaction.commit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showFragmentOne(null);
}
@Override
public void onButtonPressed(String text) {
showFragmentOne(text);
}
private void showFragmentOne(String text) {
Fragment fragment = new FragmentOne();
if (text!=null){
Bundle bundle = new Bundle();
bundle.putInt("text", text);
fragment.setArguments(bundle);
}
}
Bundle bundle = this.getArguments();
if (bundle != null) {
String text = bundle.getString("text", null);
if(text!=null) setTextt(text); // or just textView.setText(text);
}