Android 如果EditText不为空,是否可以在单击按钮时更改片段?
我有Android 如果EditText不为空,是否可以在单击按钮时更改片段?,android,android-fragments,Android,Android Fragments,我有MainActivity和FrameLayout在其中加载片段。 假设在MainActivity中有一个按钮next,用于加载下一个片段 public void btnNextClick(View view) { Fragment currentFragment; currentFragment = new F2Fragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction
MainActivity
和FrameLayout
在其中加载片段。
假设在MainActivity
中有一个按钮
next,用于加载下一个片段
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
中有一些输入时,我才需要更改片段
我尝试使用SharedReferences
和
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();
}
}
这只适用于第一次,如果我保持编辑空白并按“下一步”,则不会发生任何情况,如果我再次插入一些文本,则不会再次插入任何内容,如果我启动应用程序并插入文本,则按“下一步”即可工作
如果我有两个编辑需要检查呢
多谢各位
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");
}
}
}
是的,这是可能的,请尝试使用addTextChangedListener编辑文本。这将对您有所帮助。@waheed akhtar thanx对于我理解的答案,只需再问一个问题,如果我有两个或两个以上的编辑需要检查,这是在每次编辑中使用addTextChangedListener的好方法。很抱歉,我仍然是litle noob:)片段A和片段B是活动A的一部分,按钮位于活动上,该活动将加载下一个片段A..B..C。。。然后你想知道片段A上的编辑框是否有任何文本更改?@UzairMohammad我想加载下一个片段,如果编辑框有值,如果为空,就等于什么都没有。简单的人,使用一个在textChangelistner和im上被命中的回调,回调侦听器的实现(显然在活动A中)加载一个新片段,如果你想要我能提供的代码片段?