相关阅读:
Android开发系列二之窗口Activity的生命周期
Android开发系列一用按钮实现显示时间
设置窗口标题事件和在Activity之间跳转。
新建一个项目,新建两个Activity:MainActivity,TitleActivity ,然后再AnroidManifest.xml 中注册TitleActivity
AndroidManifest.xml内容如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.neil.ad02"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TitleActivity" android:label="@string/app_name"> </activity> </application> </manifest>
在MainActivity中的onCreate方法中
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("onCreate","onCreate Method is executed"); Button bt=(Button)findViewById(R.id.btTurnTitle); bt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent(MainActivity.this,TitleActivity.class)); } }); }
点击Button就触发TitleActivity的onCreate()->onStart()->onResume()方法
向activity_title.xml中添加一个Button,点击Button就改变app_name的值
TitleActivity中的代码
package com.neil.ad; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /** * Created by Neil on //. */ public class TitleActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_title); Button bt=(Button)findViewById(R.id.btTitle); bt.setOnClickListener((View.OnClickListener) this); setTitle("窗口标题"); } public void onClick_SetTitle(View view) { setTitle("新窗口标题"); setTitleColor(); } //在窗口完全开始后调用该方法 @Override protected void onPostCreate(Bundle savedInstanceState) { Log.d("TitleActivity","onPostCreate"); super.onPostCreate(savedInstanceState); } @Override protected void onTitleChanged(CharSequence title,int color) { super.onTitleChanged(title,color); Log.d("TitleActivity","onTitleChanged_title"+title); Log.d("TitleActivity","onTitleChanged_color"+color); } }
比较简单,就不多说了。如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!