0

FragmentActivity の Fragment 内の TextView で Text を設定する必要があります。

((TextView) findViewById(R.id.textview_in_fragment)).setText("テキスト")

FragmentActivity では機能しません。Fragment にテキストを設定する方法はわかりましたが、FragmentActivity から Fragment レイアウトにテキストを設定する必要があります。いろいろ調べましたが、直接的な答えは見つかりませんでした。

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

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>
于 2012-12-09T16:07:13.220 に答える