设置窗口标题事件和在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中的代码
1 package com.neil.ad02; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.View; 7 import android.widget.Button; 8 9 /** 10 * Created by Neil on 2016/4/30. 11 */ 12 public class TitleActivity extends Activity{ 13 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_title); 19 Button bt=(Button)findViewById(R.id.btTitle); 20 bt.setOnClickListener((View.OnClickListener) this); 21 setTitle("窗口标题"); 22 } 23 24 public void onClick_SetTitle(View view) 25 { 26 setTitle("新窗口标题"); 27 setTitleColor(12345); 28 } 29 30 //在窗口完全开始后调用该方法 31 @Override 32 protected void onPostCreate(Bundle savedInstanceState) { 33 Log.d("TitleActivity","onPostCreate"); 34 super.onPostCreate(savedInstanceState); 35 } 36 37 38 @Override 39 protected void onTitleChanged(CharSequence title,int color) 40 { 41 super.onTitleChanged(title,color); 42 Log.d("TitleActivity","onTitleChanged_title"+title); 43 Log.d("TitleActivity","onTitleChanged_color"+color); 44 } 45 46 }
比较简单,就不多说了
时间: 2024-10-04 21:34:31