我有一个片段,允许用户更改其用户名,该用户名将显示在导航抽屉内的TextView中。片段“ SettingsFragment”在MainActivity中被夸大。
SettingsFragment具有一个EditText,用户可以在其中输入其用户名和一个按钮来应用更改。导航抽屉是在MainActivity中创建的,因此我设置了SharedPreferences将用户名发送到MainActivity。然后,我继续使用.setText(username)
更改导航抽屉中的TextView,但是什么也没有发生。但是,当我重新启动该应用程序时,导航抽屉的TextView更改为用户在EditText中输入的实际用户名。
SettingsFragment.java:
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_settings, container, false);
final TextInputEditText teiUserName = view.findViewById(R.id.teiUserName);
final TextView tvEditTextSub = view.findViewById(R.id.tvEditTextSubstitute);
Button btnApplyChanges = view.findViewById(R.id.btnApplyChanges);
btnApplyChanges.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user_name = String.valueOf(teiUserName.getText());
tvEditTextSub.setText(user_name);
SharedPreferences.Editor editor = getActivity().getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("name", user_name);
editor.apply();
}
});
return view;
}}
SettingsFragment XML:
<FrameLayout 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"
android:padding="10dp"
tools:context="com.miguelpeachey.marketplacesimulator.Fragments.SettingsFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/teiUserName"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_above="@+id/btnApplyChanges"
android:layout_marginBottom="28dp"
android:text="User"
android:textColorHint="#000" />
<Button
android:id="@+id/btnApplyChanges"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:background="#33993f"
android:padding="12dp"
android:text="Apply Changes"
android:textColor="#FFF" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Fragment fragment;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
final TextView tvDrawerUsername = headerView.findViewById(R.id.tvDrawerUsername);
setSupportActionBar(toolbar);
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
final String usernameID = sharedPreferences.getString("name", "User");
if (drawer.isDrawerOpen(GravityCompat.START)) {
tvDrawerUsername.setText(usernameID);
}}
MainActivity.java中包含更多代码,但无需包含它。
在MainActivity.java中,我也尝试过:
if (drawer.isDrawerOpen(GravityCompat.START)) {
tvDrawerUsername.setText(usernameID);
}
但重新启动应用程序后,它甚至都没有更改导航抽屉的TextView(tvDrawerUsername)。
总之,在单击按钮后,该应用程序不会立即更新tvDrawerUsername,而仅在我重新启动该应用程序之后。
最佳答案
试试这个:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Fragment fragment;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
final TextView tvDrawerUsername = headerView.findViewById(R.id.tvDrawerUsername);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, null, "navigation_drawer_open", "navigation_drawer_close") {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// Do whatever you want here
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
final String usernameID = sharedPreferences.getString("name", "User");
tvDrawerUsername.setText(usernameID);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
setSupportActionBar(toolbar);
}