如何更改片段中的 TextView 值
How to change TextView Value in fragment
我正在尝试更改 Fragment 中的 TextView 值,但它没有清除旧值。
值与旧值重叠。
来自片段的代码 -
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;
}
}
代码来自 Activity -
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);
}
请帮忙....提前致谢...!
您可以将以下代码放入片段的 onCreateView 方法中,它将消除此重叠问题
View view = inflater.inflate(R.layout.your_layout, container, false);
view.setBackgroundColor(Color.WHITE);
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>
正如 Ranjan 所说,这不是好的做法,为什么不在 onCreate() 中创建 MainFragment mf = new MainFragment();
并在该方法中使用它。这样您将只有一个实例。我不使用这样的片段,所以我对你的代码不太了解。
如果您在 goToFragment()
代码中使用 .add
,则应替换为 .replace
fragmentTransaction.add(R.id.container,
currentFragment).commit()
到
fragmentTransaction.replace(R.id.fragment_container,
currentFragment).commit()
我正在尝试更改 Fragment 中的 TextView 值,但它没有清除旧值。
值与旧值重叠。
来自片段的代码 -
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;
}
}
代码来自 Activity -
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);
}
请帮忙....提前致谢...!
您可以将以下代码放入片段的 onCreateView 方法中,它将消除此重叠问题
View view = inflater.inflate(R.layout.your_layout, container, false);
view.setBackgroundColor(Color.WHITE);
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>
正如 Ranjan 所说,这不是好的做法,为什么不在 onCreate() 中创建 MainFragment mf = new MainFragment();
并在该方法中使用它。这样您将只有一个实例。我不使用这样的片段,所以我对你的代码不太了解。
如果您在 goToFragment()
代码中使用 .add
,则应替换为 .replace
fragmentTransaction.add(R.id.container,
currentFragment).commit()
到
fragmentTransaction.replace(R.id.fragment_container,
currentFragment).commit()