如何从片段本身改变片段的文本视图?

时间:2015-11-28 01:56:42

标签: java android android-fragments

我有一个MainActivity,可以创建3个碎片。一旦我进入第一个片段,我希望在此片段中更改我在片段布局中的Textview。当我尝试以这种方式访问​​TextView时,我无法在IDE中看到'findViewbyId'方法。有人可以帮帮我吗?这是Fragment的代码,我想在名为'FragmentOne'中执行该操作。代码是'FragmentOne.jav'

package com.iotaconcepts.aurum;
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.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.iotaconcepts.aurum.R;

import java.io.*;
import java.util.*;


public class OneFrangment extends Fragment
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Fragment fc=getTargetFragment();
        TextView tv=(TextView)fc.findViewById(R.id.textview);//NOT WORKING
        try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(getActivity().getAssets().open("symp.txt")));
            String parse1;
            while((parse1=br.readLine())!=null)
            {
                Toast.makeText(getActivity(), parse1, Toast.LENGTH_LONG).show();
                tv.setText(parse1 + "\n");
            }
        }
        catch(Exception e)
        {
            tv.append("wtf\n");
            Toast.makeText(getActivity(),"SORRY!", Toast.LENGTH_LONG).show();
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false);
    }
}


XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
android:w
<TextView
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Hello"
    />

1 个答案:

答案 0 :(得分:1)

您需要先将inflater.inflate方法分配给View,然后才能从中查找视图。

[value + 1 for list in list_of_lists for value in list]