• U3


    一个项目里面可以有多个Activity

    AndroidManifest.xml
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    在谁的里面就运行谁


    在两个Activity 里切换
    
    package com.example.chapter31;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.i("Activity1","onCreate");
            Button  button =(Button)findViewById(R.id.button);
            button.setOnClickListener (new View.OnClickListener(){
                @Override
                public void onClick (View view){
                    Intent intent = new Intent (MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                }});
        }
        @Override
        protected void onStart() {
            super.onStart();
            Log.i("Activity1","onStart()");
        }
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.i("Activity1","onRestart()");
        }
        @Override
        protected void onResume() {
            super.onResume();
            Log.i("Activity1","onResume()");
        }
        @Override
        protected void onPause() {
            super.onPause();
            Log.i("Activity1","onPause()");
        }
        @Override
        protected void onStop() {
            super.onStop();
            Log.i("Activity1","onStop()");
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.i("Activity1","onDestroy()");
        }
    }
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <Button
        android:id = "@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight = "1"
        android:text = "跳转到第二个Activity"  />
    
    
    
    </LinearLayout>
    
    
    
    package com.example.chapter31;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    
    public class SecondActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout1);
            Log.i("Activity1","onCreate");
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener (new View.OnClickListener(){
                @Override
                public void onClick (View view){
                    Intent intent = new Intent (SecondActivity.this,MainActivity.class);
    从2到1
                    startActivity(intent);
                }});
        }
        @Override
        protected void onStart() {
            super.onStart();
            Log.i("Activity2","onStart()");
        }
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.i("Activity2","onRestart()");
        }
        @Override
        protected void onResume() {
            super.onResume();
            Log.i("Activity2","onResume()");
        }
        @Override
        protected void onPause() {
            super.onPause();
            Log.i("Activity2","onPause()");
        }
        @Override
        protected void onStop() {
            super.onStop();
            Log.i("Activity2","onStop()");
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.i("Activity2","onDestroy()");
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">
        <Button
            android:id = "@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight = "1"
            android:text = "跳转到Activity1"  />
    
    
    
    </LinearLayout>
    
    别忘了在AndroidManifest 里面注册
  • 相关阅读:
    linux分区
    MySQL
    RGB中的颜色的设置
    解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
    解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题
    解决pycharm下安装reportLab报错的问题
    简单的爬取并下载图片的程序
    linux常用命令
    ubuntu下安装pycharm的方法
    win7下装ubuntu双系统后无法进入win7的解决方法
  • 原文地址:https://www.cnblogs.com/tingtin/p/10798090.html
Copyright © 2020-2023  润新知