How to change title of Activity in Android?

前端 未结 14 1233
无人及你
无人及你 2020-12-02 05:30

I am using

Window w = getWindow();
w.setTitle(\"My title\");

to change title of my current Activity but it does not seem to work.

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 06:07

    If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:

        private static final String TITLE_SIGN = "title_sign";
        ImageButton mAriesButton;
    

    Add onClickListener in onCreate() and make new intent for another activity:

        mTitleButton = (ImageButton) findViewById(R.id.title_button);
        mTitleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, 
            SignActivity.class);
            String title_act = getText(R.string.simple_text).toString();
            intent.putExtra("title_act", title_act);
            startActivity(intent);
            finish();
            }
        });
    

    SecondActivity code in onCreate():

        String txtTitle = getIntent().getStringExtra("title_act");
        this.setTitle(txtTitle);
    

提交回复
热议问题