安卓开发-Intent的使用

内容纲要

Intent的使用

目的

在MainActivity中输入数据,在用户点按 提交 按钮 时,启动一个新的 Activity 来显示输入的数据

具体实现

实现结构

用Student类保存输入数据,并通过Intent在组件间传输数据

Student类

public class Student {
    int Sid;
    String Sname;
    String Smajor;
    String Ssex;
    public Student(){
        this.Sid = 0;
        this.Sname = "炸炸";
        this.Smajor = "计算机";
        this.Ssex = "男";
    }
    public Student(int id, String name, String major, String sex){
        this.Sid = id;
        this.Sname = name;
        this.Smajor = major;
        this.Ssex = sex;
    }
    {getter and setter...}
}

Intend传输数据

发送{
    //创建意图
    Intent intent_mes = new Intent(this,secondary.class);
    //保存数据到Student类
    Student student = new Student(Sid,Sname,Smajor,"男");
    //向Intent放入数据
    intent_mes.putExtra("stuId",student.getSid());
    //调用
    startActivity(intent_mes);
}
接收{
    //获取意图
    Intent getIntent_mes = getIntent();
    //获取传输的数据
    int Sid = getIntent_mes.getIntExtra("stuId",0); //Int需要默认值
    //使用数据
    Toast.makeText(getApplicationContext(),"your id is " + Sid,Toast.LENGTH_SHORT).show();
}

其他结构

spinner控件的使用

  1. 添加所需String-array数组
    res > values > New > majors
    emmet语法: string-array[name="majors"]>item*5 + TAB键

    
    
        
            big data
            information manage
            computer science
            the IOT
            software
        
    

    使用数组

    //声明数组
    String[] majors;
    //指定数组
    majors = getResources().getStringArray(R.array.majors);
  2. xml布局中添加 Spinner

    设置 Entries 为@array/majors数组

  3. 使用Spinner

    //执行AdapterView的监听器接口
    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{}
    //声明并找到Spinner
    Spinner spSmajor;
    spSmajor = findViewById(R.id.spSmajor);
    //设定监听器
    spSmajor.setOnItemSelectedListener(this);
  4. 重写选择Item的函数

    
    @Override
    public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
        Smajor = majors[i];
        //Toast.makeText(getApplicationContext(),Smajor,Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onNothingSelected(AdapterView adapterView) {
    
    }

单选对话框

新建按钮,单击弹出单选对话框,保存选择的数据

case R.id.btnSex:
    Ssex = "男"; //初始化默认值
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setSingleChoiceItems(R.array.sex, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Ssex = sex[i];  //保存选择的数据项
                }
            })
            .setPositiveButton("yes",null);
    AlertDialog sexDialog = builder.create();
    sexDialog.show();
    break;

综合代码

  1. MainActivity.java

    package com.example.google_doc;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
        EditText edtSid,edtSname;
        Spinner spSmajor;
        Button btnSsex;
    
        String[] majors;
        String Smajor;
        String[] sex;
        String Ssex;
    
        Intent intent_mes;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //Spinner监听器
            spSmajor = findViewById(R.id.spSmajor);
            spSmajor.setOnItemSelectedListener(this);
            majors = getResources().getStringArray(R.array.majors);
            sex = getResources().getStringArray(R.array.sex);
        }
        public void sendStudentInfo(View view){
            switch(view.getId()){
                case R.id.btnSubmit:
                    intent_mes = new Intent(this,secondary.class);
                    //实例化对象
                    edtSid = findViewById(R.id.edtSid);
                    edtSname = findViewById(R.id.edtSname);
                    spSmajor = findViewById(R.id.spSmajor);
                    btnSsex = findViewById(R.id.btnSex);
                    //获取输入数据
                    int Sid = Integer.parseInt(edtSid.getText().toString().trim());
                    String Sname = edtSname.getText().toString();
                    //创建Student对象保存输入数据
                    Student student = new Student(Sid,Sname,Smajor,Ssex);
    
                    intent_mes.putExtra("stuId",student.getSid());   //Int
                    intent_mes.putExtra("stuName",student.getSname());   //String
                    intent_mes.putExtra("stuMajor",student.getSmajor()); //AdapterView_item
                    intent_mes.putExtra("stuSex",student.getSsex());    //String
                    //发送Message消息
                    EditText edtInfo = findViewById(R.id.edtInfo);
                    String message_tvFirst = edtInfo.getText().toString();
                    intent_mes.putExtra("myInfo",message_tvFirst);
    
                    startActivity(intent_mes);
                    break;
                case R.id.btnSex:
                    Ssex = "男"; //初始化默认值
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setSingleChoiceItems(R.array.sex, 0, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Ssex = sex[i];  //保存选择的数据项
                                }
                            })
                            .setPositiveButton("yes",null);
                    AlertDialog sexDialog = builder.create();
                    sexDialog.show();
                    break;
            }
        }
        @Override
        public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
            Smajor = majors[i];
            //Toast.makeText(getApplicationContext(),Smajor,Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onNothingSelected(AdapterView adapterView) {
    
        }
        @Override
        protected void onStart() {
            super.onStart();
            Log.i("myInfo","Main_started");
        }
    }
  2. secondary.java

    package com.example.google_doc;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class secondary extends AppCompatActivity {
        Intent getIntent_mes;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_secondary);
            receiveIntent();
        }
        public void receiveIntent() {
            getIntent_mes = getIntent();
            //接收message
            String message = getIntent_mes.getStringExtra("myInfo");
            TextView tvInfo = findViewById(R.id.tvInfo);
            tvInfo.setText(message);
    
            //接收学生信息
            int Sid = getIntent_mes.getIntExtra("stuId",0);
            String Sname = getIntent_mes.getStringExtra("stuName");
            String Smajor = getIntent_mes.getStringExtra("stuMajor");
            String Ssex = getIntent_mes.getStringExtra("stuSex");
            Toast.makeText(getApplicationContext(),"your id is " + Sid + "\n" +
                    "your name is " + Sname + "\n" + "your major is " + Smajor + "\n" +
                    "your sex is " + Ssex,Toast.LENGTH_SHORT).show();
        }
    }
  3. Student.java

    package com.example.google_doc;
    
    public class Student {
        private int Sid;
        private String Sname;
        private String Smajor;
        private String Ssex;
    
        public Student(){
            this.Sid = 0;
            this.Sname = "炸炸";
            this.Smajor = "计算机";
            this.Ssex = "男";
        }
        public Student(int id, String name, String major, String sex){
            setSid(id);
            setSname(name);
            setSmajor(major);
            setSsex(sex);
        }
        public int getSid() {
            return Sid;
        }
    
        public void setSid(int sid) {
            Sid = sid;
        }
    
        public String getSname() {
            return Sname;
        }
    
        public void setSname(String sname) {
            Sname = sname;
        }
    
        public String getSmajor() {
            return Smajor;
        }
    
        public void setSmajor(String smajor) {
            Smajor = smajor;
        }
    
        public String getSsex() {
            return Ssex;
        }
    
        public void setSsex(String ssex) {
            Ssex = ssex;
        }
    }

发表评论