Java 如何从另一个活动的片段中单击按钮打开另一个活动中包含WebView的片段?
我在一个活动的某个片段中有图像按钮,并希望在另一个活动中单击按钮时打开包含网络视图的相应片段。 我是一个初学者,所以请给这个适当的代码 活动\u main.xmlJava 如何从另一个活动的片段中单击按钮打开另一个活动中包含WebView的片段?,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我在一个活动的某个片段中有图像按钮,并希望在另一个活动中单击按钮时打开包含网络视图的相应片段。 我是一个初学者,所以请给这个适当的代码 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" xmlns:android="ht
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar">
</include>
<fragment
android:id="@+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="@id/toolbar"
>
</fragment>
<fragment
android:layout_below="@id/upperFragment"
android:id="@+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="@id/lowerFragment"
android:id="@+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="@layout/fragment_bottom_most">
</fragment>
<View
android:id="@+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="@id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/about_board"
android:id="@+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="@+id/text_about_img_btn"
android:text="About Board"
android:layout_below="@id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/sarasvati"
android:id="@+id/sarasvati_img_btn"
android:layout_toRightOf="@id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="@id/text_about_img_btn"
android:id="@+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="@id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/scientific_evidences"
android:id="@+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="@id/text_sarasvati_img_btn"
android:id="@+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="@id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/affltd_organization"
android:id="@+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="@id/text_digital_library_img_btn"
android:id="@+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="@id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/tender"
android:id="@+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="@id/text_affltd_oragnization_img_btn"
android:id="@+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="@id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
fragment\u lower\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.administrator.hiha.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar">
</include>
<fragment
android:id="@+id/upperFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.example.administrator.hiha.Upper_main_Fragment"
android:layout_below="@id/toolbar"
>
</fragment>
<fragment
android:layout_below="@id/upperFragment"
android:id="@+id/lowerFragment"
android:layout_width="match_parent"
android:layout_height="140dp"
android:name="com.example.administrator.hiha.Lower_main_Fragment"
>
</fragment>
<fragment
android:layout_below="@id/lowerFragment"
android:id="@+id/Bottom_Most_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.administrator.hiha.BottomMostFragment"
tools:layout="@layout/fragment_bottom_most">
</fragment>
<View
android:id="@+id/hLastRow"
android:layout_centerHorizontal="true"
android:layout_below="@id/Bottom_Most_Fragment"
android:layout_width="300dp"
android:layout_height="2dp"
android:background="#EEEEEE"
/>
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/hLastRow"
android:layout_centerHorizontal="true"
android:text="Designed \u0026 Developed by me © 2016."/>
</RelativeLayout>
</ScrollView>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/about_board"
android:id="@+id/about_img_btn"
android:onClick="onAboutBoardClick"
/>
<TextView
android:id="@+id/text_about_img_btn"
android:text="About Board"
android:layout_below="@id/about_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/sarasvati"
android:id="@+id/sarasvati_img_btn"
android:layout_toRightOf="@id/about_img_btn"
android:onClick="onSarasvatiClick"
/>
<TextView
android:layout_toRightOf="@id/text_about_img_btn"
android:id="@+id/text_sarasvati_img_btn"
android:text="Sarasvati"
android:layout_below="@id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/sarasvati_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/scientific_evidences"
android:id="@+id/scientific_evidences_img_btn"
android:onClick="onScientificEvidencesClick"
/>
<TextView
android:layout_toRightOf="@id/text_sarasvati_img_btn"
android:id="@+id/text_scientific_evidences_img_btn"
android:text="Scientific EVidences"
android:layout_below="@id/scientific_evidences_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/digital_library_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/affltd_organization"
android:id="@+id/affltd_oragnization_img_btn"
android:onClick="onAffltdOrganizationClick"
/>
<TextView
android:layout_toRightOf="@id/text_digital_library_img_btn"
android:id="@+id/text_affltd_oragnization_img_btn"
android:text="Affiliated Organizations"
android:layout_below="@id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
<ImageButton
android:layout_toRightOf="@id/affltd_oragnization_img_btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/tender"
android:id="@+id/tender_img_btn"
android:onClick="onTenderClick"
/>
<TextView
android:layout_toRightOf="@id/text_affltd_oragnization_img_btn"
android:id="@+id/text_tender_img_btn"
android:text="Tenders"
android:layout_below="@id/tender_img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
这些是我想要的文件,如果我点击about\u img\u btn,那么它应该在另一个活动中打开一个包含webView的片段。所有按钮都应重复此操作。
请定义我必须使用的进一步代码,并提及我必须使用它的文件。谢谢 试试这个:
在fragment\u lower\u main
xml中:
为Framelayout指定一个id:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.hiha.Lower_main_Fragment">
<FrameLayout
android:id="+@id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<--Rest of the code -->
/>
</LinearLayout>
使用
FragmentTransaction
,@Piyus我是初学者,请您定义应该在哪里使用哪些代码?检查是否需要nestead Fragments因为您说您必须为web vew打开新活动,那么为什么在另一个活动中使用fragment for webview?
about_img_btn=(ImageButton)rootView.findViewById(R.id.about_img_btn);
about_img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AboutUS fragment = new AboutUS();
Replace_fragment(fragment); //pass the fragment u want to replace
}
});
public void ReplaceFragment(Fragment r_fragment) {
FragmentManager fragmentManager = getActivity.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, r_fragment).commit();
}