0
我是新來的android和我有這個問題使用靈活的片段。 我有一個名爲mainActivity和Fragment的活動,名爲RegisteractivityFragment。onclicked靈活的片段按鈕不起作用
我在主要活動中加載片段,但是當我點擊應用程序關閉的片段按鈕時。 這是我registerActivityFragment.java的某些部分
public class RegisterActivityFragment extends Fragment {
public interface OnButtonsClicked {
public void onRegisterClicked();
public void onNextTimeclicked();
}
OnButtonsClicked buttonClickListener ;
//some codes
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
buttonClickListener = (OnButtonsClicked) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnButtonsClicked Functions");
}
}
public void clickRegister(View view)
{
buttonClickListener.onRegisterClicked();
}
public void clickNextTime(View view)
{
buttonClickListener.onNextTimeclicked();
}
}
,這是我MainActivity.java
public class MainActivity extends ActionBarActivity implements RegisterActivityFragment.OnButtonsClicked {
RegisterActivityFragment registerFragment= new RegisterActivityFragment();
@Override
public void onCreate(Bundle savedInstanceState){
//adding Fragment to activity
}
//implementing the interface
@Override
public void onRegisterClicked() {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(MainActivity.this,"asd",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
@Override
public void onNextTimeclicked() {
Toast toast = Toast.makeText(MainActivity.this,"asd",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
實際上第二種方式解決問題,但不應用封裝的面向對象的特性 – NoXSaeeD
毫米...但第一種方式是更好的 – NoXSaeeD
都工作TNX – K1A