我正在练习使用Java/XML
的Android编程。
至于下面的代码:我遇到问题的方法是OBClicked
。
OBClicked
是我在ShareFragment中定义的接口的方法(包含一个复选框和一个启动TextFragment的按钮)。
我这样做了,因为根据我所学到的,在片段中定义接口并覆盖托管活动中的接口是一种很好的方法,可以将该信息传输到另一个片段。
在这种情况下,我覆盖MainActivity中的OBClicked
方法,告诉TextFragment(它只是一个TextView)是否选中了ShareFragment中的CheckBox。根据是否已选中,我尝试使用setText告诉TextFragment显示某些文本,但这会导致程序崩溃。这是代码,任何帮助将不胜感激。 :
MainActivity:
public class MainActivity extends FragmentActivity implements ShareFragment.ClickListener{
@Override
public void oBClicked(boolean x){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
TextView t = (TextView)findViewById(R.id.text_view);
if(x == true){
t.setText("The box is checked");
}else{
t.setText("The box isn't checked");
}
Fragment fragment = new TextFragment();
ft.replace(R.id.fragmentContainer, fragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if(fragment == null){
fragment = new ShareFragment();
}
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentContainer, fragment);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ShareFragment:
public class ShareFragment extends Fragment {
Button mButton;
String mText;
Boolean mTruthValue;
CheckBox mCheckBox;
private int mCounter;
//CROSS FRAGMENT COMMUNICATION START
ClickListener mListener;
public interface ClickListener{
public void oBClicked(boolean x);
}
public void oBClicked(boolean x){
mListener.oBClicked(x);
}
//CROSS FRAGMENT COMMUNICATION FIN
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
mListener = (ClickListener) activity;
}
catch(Exception e){
//
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
View v = inflater.inflate(R.layout.fragment_share, container, false);
mButton = (Button)v.findViewById(R.id.button1);
mCheckBox = (CheckBox)v.findViewById(R.id.check_box);
mCounter = 1;
mCheckBox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
mCounter++;
}
});
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//mCheckBox = (CheckBox)v.findViewById(R.id.check_box);
if(mCounter%2 == 0){
mTruthValue = true;
}else{
mTruthValue = false;
}
oBClicked(mTruthValue);
}
});
return v;
}
}
TextFragment:
public class TextFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
View v = inflater.inflate(R.layout.fragment_text, container, false);
return v;
}
}
答案 0 :(得分:0)
TextView t = (TextView)findViewById(R.id.text_view);
if(x == true){
t.setText("The box is checked");
}else{
t.setText("The box isn't checked");
}
这会在您的活动中查找TextView。如果要在TextFragment中获取TextView,则必须在片段的onCreateView或onViewCreated中找到它。要在创建时将数据传递给片段,请使用setArguments和getArguments。
@Override
public void oBClicked(boolean x){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = new TextFragment();
Bundle arguments = new Bundle();
arguments.putBoolean("data",x);
fragment.setArguments(arguments);
ft.replace(R.id.fragmentContainer, fragment);
ft.addToBackStack(null);
ft.commit();
}
并在TextFragment的onCreateView中......
public class TextFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle save){
View v = inflater.inflate(R.layout.fragment_text, container, false);
TextView t = (TextView)v.findViewById(R.id.text_view);
Bundle arguments = getArguments();
boolean x = arguments.getBoolean("data");
if(x == true){
t.setText("The box is checked");
}else{
t.setText("The box isn't checked");
}
return v;
}