从另一个类更改getSupportActionBar()。setTitle值

时间:2013-04-13 08:16:34

标签: android actionbarsherlock

我有一个获取actionbarsherlock oncreate并设置默认值的类。如何从其他类更改此值?

不起作用的示例代码:

public class MyActivity extends SlidingFragmentActivity {
 public static String abs;

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
       setContentView(R.layout.activity_main);

       abs = "text";
       getSupportActionBar().setTitle(abs);
}
}

外部课程:

MyActivity.abs = "new value";

2 个答案:

答案 0 :(得分:2)

绝对不是推荐的设置标题的方法,但是,如果删除作业abs = "text";,它应该有效...

public class MyActivity extends SlidingFragmentActivity {
    public static String abs;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);        
       setContentView(R.layout.activity_main);

       getSupportActionBar().setTitle(abs);
    }
}

...并在第一次创建活动之前,在之前调用MyActivity.abs = "new value";

无论如何,我建议将标题作为额外的内容传递给活动......

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("title", "Another Headline");
context.startActivity(intent);

......并从意图中评估出一个:

public class MyActivity extends SlidingFragmentActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);

        Bundle extras = getIntent().getExtras();
        String title = "Default Headline";
        if (extras != null && extras.containsKey("title")) {    
            title = intent.getStringExtra("title");   
        }

        getSupportActionBar().setTitle(title);
    }
}
创建后

p.s。设置标题:

如果您可以直接引用活动,例如因为您在片段中,则可以添加类似...的方法。

public class MyActivity extends SlidingFragmentActivity {

    public void setTitle(String title) {
        this.getSupportActionBar().setTitle(title);
    }

    ....
}

...并相应地调用它:

getActivity().setTitle("Another Headline");

将任何类型的参数传递给现有活动以允许它们以任何方式处理它们的最复杂方法是​​使用BroadcastReceiver:

public abstract class MyActivity extends SlidingFragmentActivity {

    public static final String SET_TITLE_ACTION = "com.myapp.SET_TITLE_ACTION";
    public static final IntentFilter INTENT_FILTER = createIntentFilter();

    private SetTitleReceiver setTitleReceiver = new SetTitleReceiver();

    private static IntentFilter createIntentFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SET_TITLE_ACTION);
        return filter;
    }

    protected void registerSetTitleReceiver() {
        registerReceiver(setTitleReceiver, INTENT_FILTER);
    }

    protected void unRegisterSetTitleReceiver() {
        unregisterReceiver(setTitleReceiver);
    }

    public class SetTitleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(SET_TITLE_ACTION)) {
                Bundle extras = intent.getExtras();
                if (extras != null && extras.containsKey("title")) {    
                    title = intent.getStringExtra("title");   
                    this.getSupportActionBar().setTitle(title);
                }
            }
        }
    }
}

任何应该能够接收标题更改的Activity类都必须在BroadcasdtReceiver中注册onResume()并在onPause()中取消注册。这样,您可以通过从任何地方发送广播事件来设置这些活动实例的标题:

Intent intent = new Intent(MyActivity.SET_TITLE_ACTION);
intent.putExtra("title", "Another Headline");
context.sendBroadcast(intent);

希望这有帮助......干杯!

答案 1 :(得分:1)

它不起作用,因为您只是更改引用abs指向。 如果您想更改标题,请将SherlockActivity作为参数传递给您的方法。

示例:

public class MyActivity extends SherlockActivity {

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        new ChangeTitle(this).setTitle("test title");
    }
}

class ChangeTitle {
    String title;
    SherlockActivity activity;

    public ChangeTitle(SherlockActivity activity) {
        this.activity = activity;
    }

    public void setTitle(String title) {
        this.title = title;
        activity.getSupportActionBar().setTitle(this.title);
    }
}