나는이 MainActivity
와 FrameLayout
마녀 I 부하 조각이다. 말을 할 수 있다는 점에서 MainActivity
가 Button
다음 단편 그 부하, 다음
public void btnNextClick(View view) {
Fragment currentFragment;
currentFragment = new F2Fragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_switch, currentFragment);
fragmentTransaction.commit();
}
이 부분은 화면에 다음 조각을로드합니다 (이 경우 f2Fragment
) 첫 번째 조각 안에 ( f1Fragment
) EditText
필드가 있으며 입력이있는 경우에만 조각을 변경해야합니다.EditText
나는 사용하려고 노력 SharedPreferences
하고
pref=activity.getSharedPreferences("default", 0);
edt = pref.edit();
public void onPause() {
super.onPause();
edt.putString("edtText",edtText.getText().toString()).apply();
}
나중에 MainActivity
그것을 얻고 길이를 확인하려고
public void btnNextClick(View view) {
pref = this.getSharedPreferences("default",0);
String edtText = pref.getString("edtText","");
if(edtText.length()>0){
Fragment currentFragment;
currentFragment = new F2Fragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_switch,currentFragment);
fragmentTransaction.commit();
}
}
이것은 처음에만 작동합니다. 편집을 공백으로 유지하고 next
아무 일도 일어나지 않으면 텍스트를 다시 삽입하지 않으면 앱을 시작하고 텍스트를 삽입하면 다음을 누릅니다.
확인해야하는 두 가지 수정 사항이 있으면 어떻게하나요?
감사합니다.
public class ActivityActivityTablet extends Activity implements FragmentA.OnEditBoxTextLoadNewFragment{
.
.
.
//other Activity methods and your code
.
.
@Override
public void OnEditBoxTextLoadNewFragment(String textFromFragA) {
//load new fragment//if else will work if you need more than loading the fragment
}
}
조각은 다음과 같아야합니다.
public class ActivityFragment extends Fragment {
private OnEditBoxTextLoadNewFragment mListener;
//other fragment code of yours
public interface OnEditBoxTextLoadNewFragment{
public void OnEditBoxTextLoadNewFragment(String edit text);
}
EditText text= (EditText) findViewById(R.id.text1);
text1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!s.toString().equalsIgnoreCase("")) {
mListener.OnEditBoxTextLoadNewFragment(s);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnEditBoxTextLoadNewFragment) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnEditBoxTextLoadNewFragment");
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다