zoukankan      html  css  js  c++  java
  • Android——Activity跳转

    Activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.hanqi.text3.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textView" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转页面"
            android:id="@+id/mybutton"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/textView"
            android:layout_toEndOf="@+id/textView"
            android:layout_marginTop="78dp"
            android:onClick="onMyButtonClick"
            />
    </RelativeLayout>
    MainActivity.java
    package com.hanqi.text3;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    
    //继承了Activity
    public class MainActivity extends Activity {
    
        @Override //覆盖  重写了父类的onCreate()
        protected void onCreate(Bundle savedInstanceState) {
            //super  父类
            //调用了父类的方法
            super.onCreate(savedInstanceState);
            //设置Java代码和layout文件的关联
            //通过R文件的id值
            setContentView(R.layout.activity_main);
        }
        //按钮点击事件的回调函数
        public void onMyButtonClick(View v)
        {
            Log.e("ATG","按钮点击事件触发");
           // Activity2 a2= new Activity2();
            //先创建意图 Intent
            //第一个参数 来源实例, 就是当前Activity实例
            //第二个参数 目标类, 目标Activity的class
            Intent in = new Intent(this,Activity2.class);
    
            //发动意图
            startActivity(in);
    
        }
    }

    需要注意的是这个方法必须符合三个条件:1.public  2.返回void  3.只有一个参数View,这个View就是被点击的这个控件。

     
    activity2.xml
    <?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">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:text="大家好"
    
            />
        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:id="@+id/et"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转下一个"
            android:onClick="onbuttonclick"
            />
    
    </LinearLayout>

    Activity2.java

    package com.hanqi.text3;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.PersistableBundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    
    /**
     * Created by Administrator on 2016/3/16.
     */
    //回调方法
    public class Activity2 extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            //
            setContentView(R.layout.activity2);
    
            System.out.println("这是我运行的第一个Activity");
    
        //    Log.d("text", "log输出的信息");
      //      Log.w("text", "log输出的信息");
      //      Log.e("text", "log输出的信息");
     //       Log.i("text","log输出的信息");
     //       Log.v("text","log输出的信息");
    
            Log.e("TAG","######创建");
    
    
        }
        @Override
       protected void onStart(){
           super.onStart();
    
           Log.e("ATG","######启动");
    
        }
        @Override
        protected void onResume(){
            super.onResume();
            Log.e("ATG", "######显示");
        }
    
        @Override
        protected void onPause(){
            super.onPause();
            Log.e("ATG", "######暂停");
        }
        @Override
        protected void onStop(){
            super.onStop();
            Log.e("ATG", "######停止");
        }
        @Override
        protected void onRestart(){
            super.onRestart();
            Log.e("ATG", "######重新启动");
        }
        //回调方法
        //1.在暂停之后和停止之前保存
        int i=0;
        String etkey ="edittext";//成员变量
        EditText et;
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            //Bundle实际是一个Map,可以存储键值对key/value
            i++;
            Log.e("ATG","保存="+i);
    
            outState.putInt("myKey",i);
    
            super.onSaveInstanceState(outState);
    
            //保存用户的输入信息
            //EditText
            //使用id查找并获取View的实例
             et= (EditText)findViewById(R.id.et);
    
            String str=et.getText().toString();//局部变量
    
            Log.e("TAG","获取用户输入="+str);
            outState.getString("edittext",str);
    
        }
    
    
        //2.恢复销毁前的保持状态
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
    
           i = savedInstanceState.getInt("myKey");
            Log.e("ATG", "获取="+i);
    
            //恢复用户数据
            String str = savedInstanceState.getString(etkey);
            Log.e("ATG", "获取str="+str);
            //设置输入框里面的内容
            //操作View的实例
            et = (EditText)findViewById(R.id.et);
            et.setText(str);
        }
    
        @Override
        protected void onDestroy(){
            super.onDestroy();
            Log.e("ATG", "######销毁");
        }
    
    
        public void onbuttonclick(View v)
        {
            Intent in = new Intent(this,Activity3.class);
    
            startActivity(in);
        }
    
    }

    activity3.xml

    <?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">
    
    
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="关闭"
            android:onClick="onClick"
            />
    </LinearLayout>

    Activity3.java

    package com.hanqi.text3;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.PersistableBundle;
    import android.view.View;
    
    /**
     * Created by Administrator on 2016/3/19.
     */
    public class Activity3 extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
    
            setContentView(R.layout.activity3);
        }
        public void onClick(View v)
        {
            //关闭
            finish();
        }
    }

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hanqi.text3">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".Activity2"
    
                >
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    
            </activity>
            <activity android:name=".Activity3"
    
                >
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    
            </activity>
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>



  • 相关阅读:
    各国语言缩写列表,各国语言缩写-各国语言简称,世界各国域名缩写
    How to see log files in MySQL?
    git 设置和取消代理
    使用本地下载和管理的免费 Windows 10 虚拟机测试 IE11 和旧版 Microsoft Edge
    在Microsoft SQL SERVER Management Studio下如何完整输出NVARCHAR(MAX)字段或变量的内容
    windows 10 x64系统下在vmware workstation pro 15安装macOS 10.15 Catelina, 并设置分辨率为3840x2160
    在Windows 10系统下将Git项目签出到磁盘分区根目录的方法
    群晖NAS(Synology NAS)环境下安装GitLab, 并在Windows 10环境下使用Git
    使用V-2ray和V-2rayN搭建本地代理服务器供局域网用户连接
    windows 10 专业版安装VMware虚拟机碰到的坑
  • 原文地址:https://www.cnblogs.com/cuikang/p/5299754.html
Copyright © 2011-2022 走看看