• 今日所学—Android中的Activity


    一、Activity的基本操作

    活动的基本操作:

     示例:实现页面跳转功能:

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="160dp"
            android:layout_marginLeft="160dp"
            android:layout_marginTop="268dp"
            android:layout_marginBottom="64dp"
            android:text="第一个界面"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="148dp"
            android:layout_marginLeft="148dp"
            android:layout_marginBottom="340dp"
            android:text="跳转"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

    activity_main2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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=".Main2Activity">
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="160dp"
            android:layout_marginLeft="160dp"
            android:layout_marginBottom="308dp"
            android:text="结束"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="176dp"
            android:layout_marginLeft="176dp"
            android:layout_marginTop="268dp"
            android:layout_marginBottom="88dp"
            android:text="第二个界面"
            app:layout_constraintBottom_toTopOf="@+id/button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    MainActivity.java

    package com.example.activityjieshao;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        private Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(intent);
                }
            });
        }
    }

    Main2Activity.java

    package com.example.activityjieshao;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class Main2Activity extends AppCompatActivity {
        private Button butten2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            butten2=(Button)findViewById(R.id.button2);
    
            butten2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    效果图:

    二、Activity的生命周期

    三、数据的传递

     

     

     示例:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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="195dp"
            android:layout_height="114dp"
            android:layout_marginStart="124dp"
            android:layout_marginLeft="124dp"
            android:layout_marginBottom="360dp"
            android:textSize="50dp"
            android:text="跳转"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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=".Main2Activity">
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="331dp"
            android:text="TextView"
            android:textSize="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    package com.example.activityjieshao;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        private Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    //使用putExtry传递数据
    //                intent.putExtra("name","zhangsan");
    //                intent.putExtra("score",95);
    
                    //使用bundle传递数据
                   Bundle bundle=new Bundle();
                 bundle.putString("name","zhangsan");
                   bundle.putInt("score",95);
                   intent.putExtras(bundle);
    
    
                    startActivity(intent);
                }
            });
        }
    }
    package com.example.activityjieshao;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Main2Activity extends AppCompatActivity {
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            //使用putExtry接收数据
    //       Intent intent=getIntent();
    //       String name=intent.getStringExtra("name");
    //       int score=intent.getIntExtra("score",0);
    
    
    //        //使用bundle接收数据
           Bundle bundle=getIntent().getExtras();
           String name=bundle.getString("name");
           int score=bundle.getInt("score",0);
    
           textView=findViewById(R.id.textView);
    
          textView.setText("name" + name + ",score"+ score);
    
    
        }
    }

    结果:

  • 相关阅读:
    Codeforce 1335C
    CodeForces
    Codeforce:208A. Dubstep (字符串处理,正则表达式)
    HDU
    CodeForces -337A:Puzzles(水)
    Codeforce 318A
    从零教你打造免费、稳定、高效的图床
    docker-compose mysql和node连接认证mongo问题
    awardRotate转盘插件文字模糊问题和图片加载问题
    webpack从单页面到多页面
  • 原文地址:https://www.cnblogs.com/MoooJL/p/12295032.html
Copyright © 2020-2023  润新知