我最近尝试使用一个接口(interface)进行 fragment Activity 通信。这个想法是,当在 fragment 中按下按钮时,它会从同一 fragment 中的 EditText 检索数据,然后将字符串发送到 MainActivty - 这控制了我所有的 fragment - 然后启动另一个 fragment 并将字符串传递给这个 fragment 供以后使用,但是,我在最初设置发送数据的第一个接口(interface)时遇到了麻烦。不幸的是没有任何反应,因此我无法进入应该显示的下一个 fragment 。此外,我尝试过使用 getActivity() 但它无法在 fragment 中找到关联的方法,这让我相信这些 fragment 在某种程度上并没有直接连接到 MainActivity (我只掌握了 Java 的基础知识和一点点 Android,只是学习。)
我已经在下面列出了相关信息,感谢您的帮助!
fragment
public class CreateWorkoutFragment extends Fragment implements OnClickListener {
View rootViewCreateWorkoutFragment;
EditText editTextWorkoutName;
// Using an ImageView for custom button
ImageView buttonNext;
String valueCreateWorkoutEditText;
OnDataPass dataPasser;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootViewCreateWorkoutFragment = inflater.inflate(R.layout.fragment_create_workout, container, false);
buttonNext = (ImageView) rootViewCreateWorkoutFragment.findViewById(R.id.button_workout_name_next);
editTextWorkoutName = (EditText) rootViewCreateWorkoutFragment.findViewById(R.id.edit_text_workout_name);
buttonNext.setOnClickListener(this);
return rootViewCreateWorkoutFragment;
}
public interface OnDataPass {
public void onDataPass(String data);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
dataPasser = (OnDataPass) activity;
}
public void passData(String data) {
dataPasser.onDataPass(data);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_workout_name_next:
valueCreateWorkoutEditText = editTextWorkoutName.getText().toString();
passData(valueCreateWorkoutEditText);
break;
}
}
}
主要 Activity
public class MainActivity extends FragmentActivity implements OnClickListener, CreateWorkoutFragment.OnDataPass {
ImageView buttonCreate;
Fragment newFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppThemeBlue);
setContentView(R.layout.activity_main);
buttonCreate = (ImageView) findViewById(R.id.create_foreground);
buttonCreate.setOnClickListener(this);
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction tranManager = fragManager.beginTransaction();
CreateWorkoutFragment createWorkoutFrag = new CreateWorkoutFragment();
// fragment_change is just the area in XML where fragments switch
tranManager.add(R.id.fragment_change, createWorkoutFrag);
tranManager.commit();
newFragment = null;
}
@Override
public void onDataPass(String data) {
// CreateFragment is not to be confused with CreateWorkoutFragment
// CreateFragment is the fragment I'm trying to start when any strings
// are obtained from CreateWorkoutFragment
newFragment = new CreateFragment();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// create_foreground is just an ImageView used as a button
// Additionaly, other buttons are used to create other fragments,
// I've cut them out currently as they are not nessesary which is
// why CreateWorkoutFragment is only button and default currently
case R.id.create_foreground:
newFragment = new CreateWorkoutFragment();
break;
default:
newFragment = new CreateWorkoutFragment();
}
FragmentTransaction tranManager = getSupportFragmentManager().beginTransaction();
tranManager.replace(R.id.fragment_change, newFragment);
tranManager.addToBackStack(null);
tranManager.commit();
}
}
抱歉,代码不是很整洁,但是,它是从大型类中删除的最相关的代码。正如我所说,我已经尝试过其他方法,但无论哪种方式都无法从 MainActivity 获得任何响应。提前致谢!
就在我发布之前: 让应用程序向我写入 logcat 消息,它设法在单击按钮时传递数据 - 至少我认为,这与 fragment 有关没有开始!在 MainActivity>onDataPass()>new Fragment = new CreateFragment() 有什么想法吗?如前所述,其他按钮确实存在并设法更改 fragment 。但是,我们删除了代码以减少发布的代码量。
最佳答案
getActivity() but it cannot find the associated method within the fragment
这是因为 getActivity()
返回一个 Activity
,而不是您自定义子类的 MainActivity
。您可以通过类型转换轻松解决此问题。例如,在您的 fragment 中,您可以这样做:
OnDataPass main = (OnDataPass) getActivity();
main.onDataPass(message);
由于需要这样的强制转换,在我看来,界面似乎有点碍事。您可以直接转换为 MainActivity
:
MainActivity main = (MainActivity) getActivity();
main.onDataPass(message);
关于android - 从 fragment 到 Activity 问题的接口(interface)通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845164/