Android学习——Activity、Bundle

1、手动创建Activity

  学习这个的目的是明白activity创建的每一个步骤,在实际开发的时候直接创建一个activity+main.java文件即可

  步骤:①创建继承自Activity的Activity;②重写回调方法onCreate();③设置要显示的视图 setContentView(R.layout.activity_main);

  注:需要在ActivityMainifest.xml配置<activity android:name="" />

  activity通过Intent表达“意图”

  关闭activity:finish();刷新:onCreate(null);

  通过activity进行页面跳转

<?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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:id="@+id/wang_mima"
    android:textColor="#ff4500"
    android:text="忘记密码"
    />


</LinearLayout>
<?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">
<ImageButton
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:id="@+id/close"
    android:src="@drawable/close"
    android:background="#0000"
    android:layout_marginTop="15dp"
    android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入新密码"/>
    <EditText
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="新密码"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>
package com.example.learning;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.wang_mima);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,PasswordActivity.class);
                startActivity(intent);
            }
        });
    }
}
package com.example.learning;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.annotation.Nullable;

public class PasswordActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        ImageButton imageButton = findViewById(R.id.close);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

2、通过Bundle存储数据

  Bundle是键值对的组合

 

 直接用例子进行说明,返回提交的数据,效果是这样的

 

 代码:

package com.example.addinfromation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = ((EditText)findViewById(R.id.user)).getText().toString();
                String password = ((EditText)findViewById(R.id.password)).getText().toString();
                if(!"".equals(user)&&!"".equals(password)){
                    Intent intent = new Intent(MainActivity.this,InformationActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("user",user);
                    bundle.putCharSequence("password",password);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this,"请将信息填写完整",Toast.LENGTH_LONG);
                }
            }
        });
    }
}
package com.example.addinfromation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class InformationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        TextView tv_user = findViewById(R.id.user);
        TextView tv_password = findViewById(R.id.password);
        tv_user.setText(user);
        tv_password.setText(password);
    }
}

 

posted on 2022-03-30 21:43  跨越&尘世  阅读(122)  评论(0编辑  收藏  举报