Android 获取视图按钮单击ViewPager的片段

Android 获取视图按钮单击ViewPager的片段,android,android-fragments,android-viewpager,android-adapter,findviewbyid,Android,Android Fragments,Android Viewpager,Android Adapter,Findviewbyid,我已使用与ViewPager关联的FragementPagerAdapter成功设置了aFragmentActivity,以实现两个选项卡式应用程序 其中一个选项卡即“Wave”有一个文本视图和一个按钮。我只想通过按钮的onClick方法调用textview.setText方法,该方法由按钮的xml属性描述 我不知道应该在哪里初始化我的文本视图或按钮,如何获取Wave选项卡的上下文,以及应该在哪里写入onclick方法- public class InformationShow extends

我已使用与
ViewPager
关联的
FragementPagerAdapter
成功设置了a
FragmentActivity
,以实现两个选项卡式应用程序

其中一个选项卡即“Wave”有一个文本视图和一个按钮。我只想通过按钮的
onClick
方法调用
textview.setText
方法,该方法由按钮的xml属性描述

我不知道应该在哪里初始化我的
文本视图
按钮
,如何获取Wave选项卡的
上下文
,以及应该在哪里写入
onclick方法
-

public class InformationShow extends FragmentActivity {

XMLdata dataObject;

ViewPager viewPager;
    PagerAdapter adpt;


    Fragment temp;  
    TextView tv;
    Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
        viewPager=(ViewPager)findViewById(R.id.pager);
            adpt  =  new  PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adpt);
      // temp=adpt.fg.findFragmentById((int)adpt.getItemId(1));

   tv=(TextView)findViewById(R.id.graphWaveTextView);
   bt = (Button)findViewById(R.id.button1);

     }


 public void changeText(View v){

  tv.setText("It worked ");
 }
适配器类

public class PagerAdapter extends FragmentPagerAdapter {
int count = 2;
CharSequence namme[] = {"Temperature","Wave"}; 
XMLdata data;
FragmentManager fg;
public  PagerAdapter(FragmentManager fragmentManager ){
    super(fragmentManager);
    this.fg = fragmentManager;
}
Context context;

@Override
public Fragment getItem(int arg0) {

    switch (arg0){

    case 0:{    

        TemperatureGraphFrag temp = new TemperatureGraphFrag();

        return temp;

    } 


    case 1:{WaveHeightGraphFrag wave = new WaveHeightGraphFrag();

    return wave;
    } 




    }

    return null;
}






@Override
public int getCount() {
    return 2;
}



@Override
public CharSequence getPageTitle(int position) {
return namme[position];
}




    }
public class TemperatureGraphFrag extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_t, container, false);
        return view;
    }



}


public class WaveHeightGraphFrag extends Fragment  {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_sig_wave_height, container, false);


    return view;


    }




}
片段类

public class PagerAdapter extends FragmentPagerAdapter {
int count = 2;
CharSequence namme[] = {"Temperature","Wave"}; 
XMLdata data;
FragmentManager fg;
public  PagerAdapter(FragmentManager fragmentManager ){
    super(fragmentManager);
    this.fg = fragmentManager;
}
Context context;

@Override
public Fragment getItem(int arg0) {

    switch (arg0){

    case 0:{    

        TemperatureGraphFrag temp = new TemperatureGraphFrag();

        return temp;

    } 


    case 1:{WaveHeightGraphFrag wave = new WaveHeightGraphFrag();

    return wave;
    } 




    }

    return null;
}






@Override
public int getCount() {
    return 2;
}



@Override
public CharSequence getPageTitle(int position) {
return namme[position];
}




    }
public class TemperatureGraphFrag extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_t, container, false);
        return view;
    }



}


public class WaveHeightGraphFrag extends Fragment  {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_sig_wave_height, container, false);


    return view;


    }




}
fragment\u主XML
fragmentActivity

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:textColor="#65C2C9"
        android:scrollbarSize="5dp"/>


</android.support.v4.view.ViewPager>

表2片段XML图\u信号\u波\u高度

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"


     >

    <TextView
        android:id="@+id/graphWaveTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_gravity="center"


         />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"

        android:layout_gravity="center" 
        android:onClick="changeText"/>

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearTemp"
     >

    <TextView
        android:id="@+id/graphTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center"
        />

</LinearLayout>

表1片段布局XML图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"


     >

    <TextView
        android:id="@+id/graphWaveTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_gravity="center"


         />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"

        android:layout_gravity="center" 
        android:onClick="changeText"/>

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearTemp"
     >

    <TextView
        android:id="@+id/graphTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center"
        />

</LinearLayout>

将以下方法添加到您的
波高图类中:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    final TextView t = (TextView) view.findViewById(R.id.graphWaveTextView);

    Button b = (Button) view.findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            t.setText("It worked ");
        }
   });
}
这就是你想要的