Android 片段/集合文本视图中的显示方法

Android 片段/集合文本视图中的显示方法,android,android-fragments,textview,Android,Android Fragments,Textview,我在网上找不到任何关于如何将方法放入TextView的示例,以便在打开应用程序时在片段中显示它。我想我记得是这样的 + "text" + getDeviceName() + "more text" fragment_home.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

我在网上找不到任何关于如何将方法放入TextView的示例,以便在打开应用程序时在片段中显示它。我想我记得是这样的

+ "text" + getDeviceName() + "more text"
fragment_home.xml

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

<TextView
    android:id="@+id/txtLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:text="@string/overview_one"/>
</RelativeLayout>

我不清楚你在问什么。所以这里是。。。 Activity.OnCreate方法在应用程序打开时发生。但是,您可以通过示例代码将文本设置为TextView:

TextView itemText = (TextView) rootView.findViewById(R.id.txtLabel);
itemText.setText("text " + getDeviceName());
如果要在代码而不是xml中隐藏视图,请执行以下操作:

itemText.setVisibility(false);
您可以将代码放入片段类中的onCreateView方法中,除非您想创建一个新的(公共)方法。
我希望这与您的要求接近。

您能更清楚地指定它,您想要实现什么吗?我不想设置随机文本(“嗨,新文本”),我想在TextView中显示
公共字符串getDeviceName()
方法输出
text.setText(“text”+getDeviceName()+“text”)
这是我的观点,我不知道将代码放在HomeFragment.java文件的何处。我需要的代码不仅仅是您的示例所显示的代码。我再次得到答案,我编辑我的答案。您可以将代码放在onCreateView方法中。试试看是否适合你的需要。
     //I am not sure you want exactly, but 
     //Write Down a Code onCreateView
     TextView tv = (TextView) rootView.findViewById(R.id.tv);

     //Call  method write now
     String s = getDeviceName();
     tv.setText(s);
     //I am not sure you want exactly, but 
     //Write Down a Code onCreateView
     TextView tv = (TextView) rootView.findViewById(R.id.tv);

     //Call  method write now
     String s = getDeviceName();
     tv.setText(s);