0

我需要在 FragmentActivity 的 Fragment 内的 TextView 中设置文本。

((TextView) findViewById(R.id.textview_in_fragment)).setText("Text")

在 FragmentActivity 中不起作用。我发现了如何在 Fragment 中设置文本,但我需要在 FragmentActivity 的片段布局中设置文本。我搜索了很多,但我找不到直接的答案。

4

1 回答 1

1

您可以尝试在下面编写代码,

只需在您的片段中创建和方法来设置它的值

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

在此处输入图像描述

基本片段

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

基本片段活动

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

    }
}

活动片段

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

片段按钮

<?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>
于 2012-12-09T16:07:13.220 回答