我最近嘗試過使用接口進行片段活動通信。這個想法是,當一個按鈕在一個片段中被按下時,它從同一片段中的EditText中檢索數據,然後將該字符串發送給MainActivty - 這將控制我的所有片段 - 然後啓動另一個片段並將字符串傳遞給此片段供以後使用,但是,我在初始設置發送數據的第一個接口時遇到了問題。不幸的是,沒有任何事情發生,我不能去下一個應該顯示的片段。此外,我嘗試使用getActivity(),但它無法在片段中找到關聯的方法,導致我相信這些片段不會直接連接到MainActivity(我只掌握了Java和一點Android的基礎知識,只是學習。)從片段到活動問題的接口通信
我已經列出了下面的相關信息,感謝您的幫助!
片段
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;
}
}
}
主要活動
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的信息給我,它設法通過單擊按鈕時數據 - 至少我認爲,是什麼做的片段無法啓動!在MainActivity> onDataPass()> new Fragment = new CreateFragment()任何想法?如前所述,其他按鈕確實存在並設法更改片段。但是,爲了減少發佈的代碼數量,
我的意思不是聽起來像一個白癡,但我會在哪裏用'main.onDataPass(消息);'有關我的代碼?你的意思是完全取消接口,而是使用getActivty()或保留接口,同時做一些小的改變? – Rossw19
@ Rossw19編輯了一些更多的細節和澄清。 –
感謝您的幫助,我現在明白您在談論什麼,因爲我之前在嘗試返回主要活動時遇到類似問題,但是,再次,使用任一解決方案都沒有發生任何事情。 – Rossw19