2017-10-16 12 views
0

Je suis confronté à un problème concernant l'écran d'introduction dans Android. Je crée button dans un fragment mais j'essaie d'appeler ce bouton dans welcomeActivity à layoutinflate en activité mais il n'effectue aucune fonctionnalité.Introduction Écran Bouton ViewPager

XML Fragment (custom_intro):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
> 
    <Button 
     android:id ="@+id/explore" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:text="Explore" 
     android:background="@drawable/round_button" 
     android:padding="15dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="114dp" /> 
</RelativeLayout> 

WelcomeActivityLayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorPrimaryDark"> 
    <!--tools:showIn="@layout/welcome_activity"--> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 


</RelativeLayout> 

WelcomeActivityJava:

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.welcome_activity); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     customView = inflater.inflate(R.layout.custom_intro, null); 
Button button=(Button)customView.findViewById(R.id.explore); 
button.setText("Test"); 

Int[] layouts = new int[]{ 
       R.layout.custom_intro, 
       R.layout.custom_intro1, 
       R.layout.custom_intro2, 
       R.layout.custom_intro3, 
     R.layout.custom_intro4}; 


     myViewPagerAdapter = new MyViewPagerAdapter(); 
     viewPager.setAdapter(myViewPagerAdapter); 
     viewPager.addOnPageChangeListener(viewPagerPageChangeListener); 
     viewPagerIndicator.setupWithViewPager(viewPager); 
     viewPagerIndicator.addOnPageChangeListener(viewPagerPageChangeListener); 
} 

Répondre

0

Si vous voulez accéder au bouton de Fragment l'intérieur de son activité parent alors vous devriez définissez une méthode dans votre classe Fragment comme ceci:

public class MyFragment extends Fragment { 

TextView mTextView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.activity_main, container, false); 
    mButton = (Button) view.findViewById(R.id.textView1); 
    return view; 
} 

public void setButtonText(String value){ 
    mTextView.setText(value); 
} 
} 

Ensuite, vous pouvez utiliser dans votre activité comme ceci:

myFragment.setButtonText("Test"); 
+0

si je veux appeler deuxième fragment par ce bouton –

+0

si vous voulez appeler un autre fragment du fragment courant \t \t \t Public void callNextFragment() { NextFragment nextFrag = new NextFragment(); . getActivity() getSupportFragmentManager() beginTransaction() de .replace (R.id.Layout_container, nextFrag, "findThisFragment") .addToBackStack (null) de .commit(). } –

+0

appelle cette méthode à partir de l'activité parente –