码迷,mamicode.com
首页 > 移动开发 > 详细

(Android学习系列)三,窗口的常用事件

时间:2016-04-30 18:19:20      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

设置窗口标题事件和在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 }

比较简单,就不多说了

(Android学习系列)三,窗口的常用事件

标签:

原文地址:http://www.cnblogs.com/jnzdn/p/5449056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!