2017-02-22 88 views
2

我在一个活动的片段之一中有图像按钮,并希望在另一个活动的按钮点击中打开包含webViews的相应片段。 我是初学者,所以请给与适当的代码。如何在另一个活动上打开包含webViews的片段按钮单击其他活动的片段?

activity_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> 

MainActivity.java

package com.example.administrator.hiha; 

import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.Toast; 

public class MainActivity extends FragmentActivity { 

//Toolbar toolbar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //toolbar=(Toolbar)findViewById(R.id.toolbar); 

    } 
} 

fragment_lower_main.xml

<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> 

Lower_Main_Fragment.java

package com.example.administrator.hiha; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 

public class Lower_main_Fragment extends Fragment { 

    ImageButton about_img_btn; 
    ImageButton sarasvati_img_btn; 
    ImageButton scientific_evidences_img_btn; 
    ImageButton digital_library_img_btn; 
    ImageButton affltd_oragnization_img_btn; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView=inflater.inflate(R.layout.fragment_lower_main_,container,false); 

     //cast image buttons 
     about_img_btn=(ImageButton)getView().findViewById(R.id.about_img_btn); 
     sarasvati_img_btn=(ImageButton)getView().findViewById(R.id.sarasvati_img_btn); 
     scientific_evidences_img_btn=(ImageButton)getView().findViewById(R.id.scientific_evidences_img_btn); 
     digital_library_img_btn=(ImageButton)getView().findViewById(R.id.digital_library_img_btn); 
     affltd_oragnization_img_btn=(ImageButton)getView().findViewById(R.id.affltd_oragnization_img_btn); 

     return rootView; 

    } 

    //onclick methods of image buttons 
    public void onAboutBoardClick(View view){ 

    } 
} 

这些都是在我想,如果我点击about_img_btn那么它应该打开包含在另一活动网页视图片段的文件。对于所有按钮应该重复相同的操作。 请定义我必须使用的进一步代码,并提及必须使用它的文件。谢谢!使用单击按钮时

<?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="[email protected]/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <--Rest of the code --> 
    /> 
</LinearLayout> 

现在在代码开放的新片段:

+0

使用'FragmentTransaction', – Piyush

+0

@Piyush我是一个初学者所以,你可以定义哪些代码应该用在哪里? –

+0

检查[this](http://stackoverflow.com/questions/23212162/how-to-move-from-one-fragment-to-another-fragment-on-click-of-a-imageview-in-and) – Piyush

回答

1

试试这个:

fragment_lower_main XML

给一个ID,FrameLayout里

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(); 
} 
相关问题