如何更改片段中的TextView值

时间:2019-02-12 11:36:45

标签: android android-studio android-fragments textview

我正在尝试更改Fragment中的TextView值,但不会清除旧值。

值与旧值重叠。

enter image description here

片段中的代码-

public class MainFragment extends Fragment {

TextView t1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    t1 = view.findViewById(R.id.textview1);
    t1.setText(s1);

    return view;
}
}

活动中的代码-

public static String s1;
@Override
public void onOptionClicked(int position, Object objectClicked) {
    // Set the toolbar title
    setTitle(mTitles.get(position));

    // Set the right options selected
    mMenuAdapter.setViewSelected(position, true);
    s1 = mTitles.get(position);
    // Navigate to the right fragment
    switch (position) {
        default:
            goToFragment(new MainFragment(), false);
            break;
    }
    // Close the drawer
    mViewHolder.mDuoDrawerLayout.closeDrawer();
}

片段xml文件-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>

尝试使用下面的代码但不起作用。...

public void setText(String text){
        TextView textView = (TextView) getView().findViewById(R.id.detailsText);
        textView.setText(textview1);
    }

请帮助...。在此先感谢...!

4 个答案:

答案 0 :(得分:0)

您可以将下面的代码放在片段的onCreateView方法中,这将消除此重叠的问题

View    view = inflater.inflate(R.layout.your_layout, container, false);
view.setBackgroundColor(Color.WHITE);

答案 1 :(得分:0)

fragment.xml在您的布局中添加(android:background =“ @ color / white”)。

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"      
android:orientation="vertical">

<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>

答案 2 :(得分:0)

正如Ranjan所说的那样,这不是一个好习惯,为什么不在onCreate()中创建MainFragment mf = new MainFragment();并在该方法中使用它。这样,您将只有一个实例。我不使用这样的片段,否则我对您的代码不了解。

答案 3 :(得分:0)

如果您在.add代码中使用goToFragment(),则应替换为.replace

fragmentTransaction.add(R.id.container,
             currentFragment).commit()

fragmentTransaction.replace(R.id.fragment_container,
             currentFragment).commit()