我有一个Fragment,允许用户更改其用户名,该用户名将显示在导航抽屉内的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,而只是在我重新启动该应用程序之后。
答案 0 :(得分:1)
尝试这个:
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);
}
答案 1 :(得分:0)
尝试在TextView
类定义上定义SettingsFragment
属性:
public class SettingsFragment extends Fragment {
TextView tv;
public SettingsFragment() {
// Required empty public constructor
}
在同一类上为此属性定义一个设置器:
public void setTextView(TextView tv){
this.tv = tv
}
从您的活动开始,在初始化fragment
和textView
之后,通过刚刚创建的设置器传递textView
:
fragment = new SettingsFragment();
fragment.setTextView(tvDrawerUsername);
最后在您的textView.setText(user_name);
方法内添加onlick()
:
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();
textView.setText(user_name);
}
});
希望这会有所帮助