2012-12-09 54 views
0

我需要在FragmentActivity中的Fragment中的TextView中設置TextText。從FragmentActivity處理Fragment中的TextView

((的TextView)findViewById(R.id.textview_in_fragment))的setText( 「文本」)

在FragmentActivity犯規作品。我發現如何在Fragment中設置文本,但我需要從FragmentActivity的Fragment佈局中設置文本。我搜查了很多,但我找不到直接的答案。

+0

的'Fragment'處於'ViewPager'?否則,只需使用'FragmentManager'來查找'Fragment'並調用更新TextView的更新文本方法。 – Luksprog

回答

1

你可以試試下面的代碼,

剛剛創建,並在您fragmet方法來設置它

public void setText(String text) 
{ 
    button.setText(text); 
} 

enter image description here

值BasicFragment

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 

public class BasicFragment extends Fragment { 
    Button button; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_basic, container, false); 
     button = (Button) view.findViewById(R.id.fragment_button); 
     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Activity activity = getActivity(); 

       if (activity != null) { 
        Toast.makeText(activity, 
          "toast_you_just_clicked_a_fragment", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 

     }); 

     return view; 
    } 

    public void setText(String text){ 
     button.setText(text); 
    } 
} 

BasicFragmentActivity

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class BasicFragmentActivity extends FragmentActivity { 
    BasicFragment b; 
    Button btn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_fragment); 
     btn = (Button) findViewById(R.id.button1); 

     btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       b.setText("test");   
      } 
     }); 

     FragmentManager fm = getSupportFragmentManager(); 
     Fragment fragment = fm.findFragmentById(R.id.fragment_content); 

     if (fragment == null) { 
      b = new BasicFragment(); 
      FragmentTransaction ft = fm.beginTransaction(); 
      ft.add(R.id.fragment_content, b); 
      ft.commit(); 
     } 

    } 
} 

activity_fragment

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

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

    <FrameLayout 
     android:id="@+id/fragment_content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </FrameLayout> 

</LinearLayout> 

fragment_button

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" android:background="#ffffaa"> 

    <Button 
     android:id="@+id/fragment_button" 
     android:text="BtnFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
相關問題