设置所有 activity 操作栏文本

Setting all activity actionbar text

在我的主 activity 中,我有一个编辑文本字段,其中输入的任何内容都会更改操作栏文本。

我需要存储该值并将其添加到我的所有其他活动中,以便当用户输入文本时它会更改所有操作栏。

这是更改设置操作栏文本的代码,但我需要获取存储在 getSupportActionBar().setTitle(editTextHouse.getText().toString()); 中的值并将其添加到所有其他活动。

        buttonSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            getSupportActionBar().setTitle(editTextHouse.getText().toString());
            }
        });

试试这个:

        private static final String NAME = "my_pref";
        private static final String ACTION_BAR_TITLE = "action_bar_title";
        private SharedPreferences sharedPreferences;
        private SharedPreferences.Editor editor;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);


          }
...
buttonSet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                String title = editTextHouse.getText().toString();
                getSupportActionBar().setTitle(title);
                editor = sharedPreferences.edit();
                editor.putString(ACTION_BAR_TITLE, title);
                editor.apply();
                }
            });

...

然后在另一个activity中得到一个标题,像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
                        String title = sharedPreferences.getString(ACTION_BAR_TITLE, "Default Title"); // your title
                        getSupportActionBar().setTitle(title);

                  }