首先我很抱歉,如果已经被问过这个问题。好吧,我将应用程序设置到带有三个片段的导航抽屉中。我希望能够通过按下按钮从其中一个片段中打开一个新活动。感谢。
这是片段的代码,我想要一个按钮,在xml中定义,以便将我发送给新的活动。
package com.devdunn.tankdicionary;
import com.devdunn.tankdictionary.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment_USA extends Fragment {
private AdView adView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_usa, container, false);
adView = (AdView) rootView.findViewById(R.id.adViewUsa);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("BE789CF61F8CB8D02BC1F84BA8EB6E24")
.build();
adView.loadAd(adRequest);
return rootView;
}
}
答案 0 :(得分:0)
如果你想从另一个FragmentActivity调用FragmentActivity,那么试试这个
public void onClick2(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
如果您想从片段中打开一个Activity,请尝试以下代码,
Intent myIntent = new Intent((ParentActivity)getActivity(), YourOtherActivity.class);
(ParentActivity)getActivity().startActivity(myIntent);
只需使用此内部按钮的点击事件。