2012-12-09 23 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> 
相关问题