Android开发系列三之窗口的常用事件
相关阅读:
Android开发系列二之窗口Activity的生命周期
Android开发系列一用按钮实现显示时间
设置窗口标题事件和在Activity之间跳转。
新建一个项目,新建两个Activity:MainActivity,TitleActivity,然后再AnroidManifest.xml中注册TitleActivity
AndroidManifest.xml内容如下
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns: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"> <activityandroid:name=".MainActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activityandroid:name=".TitleActivity" android:label="@string/app_name"> </activity> </application> </manifest>
在MainActivity中的onCreate方法中
@Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("onCreate","onCreateMethodisexecuted"); Buttonbt=(Button)findViewById(R.id.btTurnTitle); bt.setOnClickListener(newView.OnClickListener(){ publicvoidonClick(Viewv){ startActivity(newIntent(MainActivity.this,TitleActivity.class)); } }); }
点击Button就触发TitleActivity的onCreate()->onStart()->onResume()方法
向activity_title.xml中添加一个Button,点击Button就改变app_name的值
TitleActivity中的代码
packagecom.neil.ad; importandroid.app.Activity; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.View; importandroid.widget.Button; /** *CreatedbyNeilon//. */ publicclassTitleActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_title); Buttonbt=(Button)findViewById(R.id.btTitle); bt.setOnClickListener((View.OnClickListener)this); setTitle("窗口标题"); } publicvoidonClick_SetTitle(Viewview) { setTitle("新窗口标题"); setTitleColor(); } //在窗口完全开始后调用该方法 @Override protectedvoidonPostCreate(BundlesavedInstanceState){ Log.d("TitleActivity","onPostCreate"); super.onPostCreate(savedInstanceState); } @Override protectedvoidonTitleChanged(CharSequencetitle,intcolor) { super.onTitleChanged(title,color); Log.d("TitleActivity","onTitleChanged_title"+title); Log.d("TitleActivity","onTitleChanged_color"+color); } }
比较简单,就不多说了。如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!