zoukankan      html  css  js  c++  java
  • Android Activity

    Activity是一个应用程序组件,提供了用户与程序交互的界面
    Android四大组件:
     -- Activity
     -- Service
     -- BroadcastReceiver
     -- Content Provider
    Activity如何创建使用
     首先创建一个类继承Android的Activity类;
     重写方法;
     设置显示布局;
     在AndroidManifest文件中,注册Activity。
    新建一个项目ActivityDemo,项目创建出来后会创建一个MainActivity类,MainActivity类继承自Activity,同时他会重写onCreate()方法,通过setContentView()函数将页面显示出来。
    Activity需要在AndroidManifest.xml中进行注册:
    <activity
                android:name="com.example.autocompletetextviewandmulti.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    这里,MainActivity在项目创建时自动注册了。
    如果该Activity是当前程序的主入口,则还要为其设置action和category属性。

    Activity的生命周期
    onCreate(); 创建
    onStart(); 运行
    onResume(); 获取焦点
    onPause(); 失去焦点
    onStop(); 暂停
    onDestroy(); 销毁
    onRestart(); 

    Activity的四种状态
     -- 活动状态(Active/Running) Activity处于界面最顶端,获取焦点
     -- 暂停状态(Paused) Activity失去焦点,但对用户可见
     -- 停止状态(Stopped) Activity被王权遮挡,但保留所有状态和成员信息
     -- 非活动状态(Killed) Activity被停止
    从创建到销毁的生命周期:
    onCreate() --> onStart() --> onResume() --> onPause() --> onStop() --> onDestroy()
    在MainActivity类中重写以上方法,运行Android App,在LogCat中能够看到输出结果。
    从启动到后台,再到前台的生命周期
    onCreate()->onStart()->onResume()->onPause()->onStop()->onRestart()->onStart()->onResume()
    停止状态(Stopped)Activity被完全遮挡,但保留所有状态和成员信息。
    从启动到失去焦点,再到获取焦点的生命周期
    onCreate()->onStart()->onResume()->onPause()->onResume()
    为了进行这个操作我们需要新建一个布局类SecondActivity;
    再在layout/目录下新建一个布局second_view.xml;
    再在SecondActivity的onCreate方法中设定setContentView(R.layout.second_view);
    还需要在AndroidManifest.xml文件中注册一下。
    <activity
                android:name="com.example.activitydemo.SecondActivity"
       android:theme="@android:style/Theme.DeviceDefault.Dialog"
       >
            </activity>
    这样这个SecondActivity就能用了,并设置SecondActivity的样式。
    在activity_main.xml中新建一个按钮用于从MainActivity中转到SecondActivity。
    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    在MainActivity中给该按钮设置点击事件
    在监听事件同通过intent进入了新的焦点。
    @Override
    public void onClick(View arg0) {
     Intent intent = new Intent(MainActivity.this, SecondActivity.class);
     MainActivity.this.startActivity(intent);
    }

    package com.example.activitydemo;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends ActionBarActivity {
        
        final String TAG = "tag";
        private Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
            Log.i(TAG, "MainActivity --> onCreate");
            button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    MainActivity.this.startActivity(intent);
                }
                
            });
        }
    
        @Override
        protected void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            Log.i(TAG, "MainActivity --> onStart");
        }
        
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            Log.i(TAG, "MainActivity --> onResume");
        }
        
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            Log.i(TAG, "MainActivity --> onPause");
        }
        
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            Log.i(TAG, "MainActivity --> onStop");
        }
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.i(TAG, "MainActivity --> onDestroy");
        }
    
    }
    MainActivity.java
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这个类用于演示Activity及其生命周期" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>
    activity_main.xml
    package com.example.activitydemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class SecondActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_view);
        }
        
    }
    SecondActivity.java
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是第二个Activity" />
    
    </LinearLayout>
    second_view.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.activitydemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.activitydemo.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.activitydemo.SecondActivity"
                android:theme="@android:style/Theme.Dialog"
                >
            </activity>
        </application>
    
    </manifest>
    AndroidManifest.xml
  • 相关阅读:
    世界上最帅的人是谁?
    Java 常量池存放的是什么
    刚 安装 Oracle时,登录会出现的问题, ora-28000: the account is locked
    使用MyBatis Generator自动创建代码
    1.2---翻转字符串(CC150)
    1.1---判断字符串是否所有字符都不相同(CC150)
    1.8---字符串是否是旋转而成(CC150)
    1.7---将矩阵元素为0的行列清零0(CC150)
    String和StringBuffer的转换
    Linux下端口被占用解决
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5406344.html
Copyright © 2011-2022 走看看