Activity之间的参数传递(Android Studio学习笔记5)

Activity之间的参数传递


一、从另一个Activity中,获得的不同的电话号码,实现电话拨打

通过点击DialActivity的文本框跳转到GetPhoneActivity,在GetPhoneActivity中输入电话号码并返回给DialActivity,最后进行拨号.


1. 界面:

Activity之间的参数传递(Android Studio学习笔记5)

Activity之间的参数传递(Android Studio学习笔记5)


2. 代码

在点击DialActvity的文本框后跳转到GetPhoneActivity

phoneNum.setOnClickListener((v)->{
    Intent intent = new Intent();
    intent.setClass(DialActivity.this, GetPhoneActivity.class);
    startActivityForResult(intent, 0);
});

GetPhoneActivity中返回电话号码:

public class GetPhoneActivity extends AppCompatActivity {

    EditText editText_getPhone;
    Button btn_get;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_phone);

        editText_getPhone = findViewById(R.id.editText_getPhone);
        btn_get = findViewById(R.id.btn_get);

        btn_get.setOnClickListener((v) -> {
            Intent intent = new Intent();
            String phoneNum = editText_getPhone.getText().toString();
            intent.putExtra("phoneNum",phoneNum);
            setResult(0,intent);
            finish();
        });

    }
}

Activity之间的参数传递(Android Studio学习笔记5)


DialActivity中重写onActivityResult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if(data == null) return;
    Bundle bundle = data.getExtras();
    if (requestCode == 0){
        String phoneNum = (String) bundle.get("phoneNum");
        editText_phoneNum.setText(phoneNum);
    }
}

3. 演示

Activity之间的参数传递(Android Studio学习笔记5)

4. 总代码

DialActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class DialActivity extends AppCompatActivity {

    EditText editText_phoneNum;
    Button btn_call;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dial);

        if (shouldAskPermissions()){
            askPermissions();
        }

        editText_phoneNum = findViewById(R.id.editText_phoneNum);
        btn_call = findViewById(R.id.btn_call);

        editText_phoneNum.setOnClickListener((v)->{
            Intent intent = new Intent();
            intent.setClass(DialActivity.this, GetPhoneActivity.class);
            startActivityForResult(intent, 0);
        });

        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + editText_phoneNum.getText()));
                startActivity(intent);
            }
        });

    }

    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }

    protected void askPermissions(){
        String[] permissions = {
                "android.permission.CALL_PHONE"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(data == null) return;
        Bundle bundle = data.getExtras();
        if (requestCode == 0){
            String phoneNum = (String) bundle.get("phoneNum");
            editText_phoneNum.setText(phoneNum);
        }
    }

}


二、打开相机拍照后,显示在图片框

点击打开相机并拍照后,照片在下方显示


1. 界面

Activity之间的参数传递(Android Studio学习笔记5)


2. 代码

点击打开相机后,跳转到相机(需要先获取权限):

btn_openCam.setOnClickListener((v) -> {
    Intent intent = new Intent();
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
});

重写onActivityResult方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data == null) return;
    Bundle bundle = data.getExtras();
    if (requestCode == REQUEST_CAMERA){
        Bitmap b = (Bitmap) bundle.get("data");
        imageView_photo.setImageBitmap(b);
    }
}

3. 演示

Activity之间的参数传递(Android Studio学习笔记5)


4. 总代码

CameraActivity:

package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;

public class CameraActivity extends AppCompatActivity {

    Button btn_openCam;
    ImageView imageView_photo;

    private final static int REQUEST_CAMERA = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        if (shouldAskPermissions()){
            askPermissions();
        }

        btn_openCam = findViewById(R.id.btn_openCam);
        imageView_photo = findViewById(R.id.imageView_photo);

        btn_openCam.setOnClickListener((v) -> {
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, REQUEST_CAMERA);
        });

    }

    protected boolean shouldAskPermissions(){
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }

    protected void askPermissions(){
        String[] permissions = {
                "android.permission.CAMERA"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null) return;
        Bundle bundle = data.getExtras();
        if (requestCode == REQUEST_CAMERA){
            Bitmap b = (Bitmap) bundle.get("data");
            imageView_photo.setImageBitmap(b);
        }
    }
}


三、添加和显示学生信息

StudentActivity中添加多个学生后,在ShowStudentActicity中显示


1. 界面

Activity之间的参数传递(Android Studio学习笔记5)

Activity之间的参数传递(Android Studio学习笔记5)


2. 代码

Student类:
Activity之间的参数传递(Android Studio学习笔记5)


StudentActivity创建Student的列表,并在点击添加学生后,在列表中添加:

ArrayList<Student> students = new ArrayList<Student>();
btn_addStu.setOnClickListener((v) -> {
    String stuName = String.valueOf(editText_stuName.getText());
    String stuMajor = String.valueOf(editText_stuMajor.getText());
    students.add(new Student(stuName, stuMajor));
    editText_stuName.setText("");
    editText_stuMajor.setText("");
});

点击显示学生后,传送学生列表:

btn_showStu.setOnClickListener((v) -> {
    Intent intent = new Intent();
    intent.setClass(StudentActivity.this, ShowStudentActivity.class);
    intent.putExtra("students", students);
    startActivity(intent);
});


ShowStudentActivity中显示学生:

Bundle bundle = getIntent().getExtras();
ArrayList<Student> students = (ArrayList<Student>) bundle.get("students");
for (Student s: students){
    textView_showStu.append("姓名:" + s.name + ";专业:" + s.major + "\n");
}

3. 演示

Activity之间的参数传递(Android Studio学习笔记5)


4. 总代码

Student:

package com.example.myapplication;

import java.io.Serializable;

public class Student implements Serializable {

    public String name;
    public String major;

    public Student(String name, String major) {
        this.name = name;
        this.major = major;
    }
}

StudentActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;


public class StudentActivity extends AppCompatActivity {

    EditText editText_stuName;
    EditText editText_stuMajor;
    Button btn_addStu;
    Button btn_showStu;

    ArrayList<Student> students = new ArrayList<Student>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);

        editText_stuName = findViewById(R.id.editText_stuName);
        editText_stuMajor = findViewById(R.id.editText_stuMajor);
        btn_addStu = findViewById(R.id.btn_addStu);
        btn_showStu = findViewById(R.id.btn_showStu);

        btn_addStu.setOnClickListener((v) -> {
            String stuName = String.valueOf(editText_stuName.getText());
            String stuMajor = String.valueOf(editText_stuMajor.getText());
            students.add(new Student(stuName, stuMajor));
            editText_stuName.setText("");
            editText_stuMajor.setText("");
        });

        btn_showStu.setOnClickListener((v) -> {
            Intent intent = new Intent();
            intent.setClass(StudentActivity.this, ShowStudentActivity.class);
            intent.putExtra("students", students);
            startActivity(intent);
        });


    }
}

ShowStudentActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;

public class ShowStudentActivity extends AppCompatActivity {

    TextView textView_showStu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_student);

        textView_showStu = findViewById(R.id.textView_showStu);

        Bundle bundle = getIntent().getExtras();
        ArrayList<Student> students = (ArrayList<Student>) bundle.get("students");
        for (Student s: students){
            textView_showStu.append("姓名:" + s.name + ";专业:" + s.major + "\n");
        }

    }
}
上一篇:AJAX---GET设置请求参数


下一篇:测试开发之前端篇-CSS层叠式样式表