2017-10-16 107 views
0

我正面临有关Android中的介绍屏幕的问题。 我在一个片段中创建button,但我试图在活动中调用welcomeActivitylayoutinflate中的此按钮,但它不执行任何功能。介绍屏幕按钮ViewPager

XML片段(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); 
} 

回答

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

如果你想从当前片段调用另外一个片段叫第二个片段是什么\t \t \t public void callNextFragment(){ NextFragment nextFrag = new NextFragment(); ();();();}();}} } –

+0

从父活动调用此方法 –