0

私はアンドロイドでイントロ画面に関する問題に直面しています。 私は1つの断片でbuttonを作成するが、私は活動にlayoutinflateを通じてwelcomeActivityにこのボタンを呼び出すようにしようとしているが、それは任意の機能を実行しません。紹介画面ボタンViewPager

XMLフラグメント(custom_intro):WelcomeActivityLayout

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

<?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); 
} 

答えて

0

あなたは、その親アクティビティ内のフラグメントのボタンにアクセスしたい場合は、あなたがしなければなりません次のように、Fragmentクラス内のメソッドを定義します。

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); 
} 
} 

その後は、このようなあなたのアクティビティの内側に、これを使用することができます。

myFragment.setButtonText("Test"); 
+0

私はあなたが現在のフラグメントから別のフラグメントを呼び出したい場合は、このボタン –

+0

を介して第2の断片を呼び出したい場合はどのような\t \t \t 公共ボイドcallNextFragment(){ NextFragment nextFrag =新しいNextFragment()。 (null) getActivity()。getSupportFragmentManager()。beginTransaction() 。 } –

+0

親アクティビティからこのメソッドを呼び出します –