我正在使用android studio编写一个android应用程序。到目前为止,我已经创建了一个选项卡式活动作为我的主要活动,而另一个活动将通过单击主选项卡式活动中一个选项卡中的一个按钮来打开。问题是我试图通过几种方法来完成此操作,但是看来Number Of Chars In..CSCI_1136_Lab6.docx File : 5923
Number Of Alph+Numeric Chars In..CSCI_1136_Lab6.docx File : 1758
Number Of Alphabet Chars In..CSCI_1136_Lab6.docx File : 1550
Number Of Numeric Chars In..CSCI_1136_Lab6.docx File : 208
Number Of Words In..CSCI_1136_Lab6.docx File : 66
Number Of Lines In..CSCI_1136_Lab6.docx File : 48
0
中的代码根本没有执行。这是我要运行的代码:
public View onCreateView
我还尝试向此代码块添加简单的Toast消息,以查看它是否被执行,但我也没有看到Toast弹出...
答案 0 :(得分:0)
首先,将Activity
委托给startActivity()
。在片段上尝试以下操作:
public class FragmentMain extends Fragment {
private OnInteractionListener mListener;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button mainSignInButton = (Button) view.findViewById(R.id.mainSignInButton);
mainSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClickSignInButton()
}
});
return view;
}
public interface OnInteractionListener {
void onClickSignInButton();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null
}
}
在“活动”上尝试执行以下操作:
public class MyActivity extends AppCompatActivity implements FragmentMain.OnInteractionListener {
...
@Override
public void onClickSignInButton() {
Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
}
}