Android : Intent(意图)_页面跳转、传递数据_简单应用

示例图:

Android : Intent(意图)_页面跳转、传递数据_简单应用_第1张图片

MainActivity.java

package com.example.myintent;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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


public class MainActivity extends AppCompatActivity {

    private Button btnLogin,btnReset,btnSum;
    private TextView textView;


    private EditText etUserName,etPassWord;
    private EditText etNumberOne,etNumberTwo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取视图中的组件
        btnLogin = findViewById(R.id.btn_login);
        btnReset = findViewById(R.id.btn_reset);
        btnSum = findViewById(R.id.btn_sum);

        textView =findViewById(R.id.tv_result);

        etUserName = findViewById(R.id.btn_user_name);
        etPassWord = findViewById(R.id.btn_pass_word);

        etNumberOne = findViewById(R.id.btn_number_one);
        etNumberTwo = findViewById(R.id.btn_number_two);


        //登录事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //信息认证
                if(etUserName.getText().toString().equals("admin") && etPassWord.getText().toString().equals("123456")){
                    //创建意图 跳转页面 1
//                Intent intent = new Intent();
//                intent.setClass(MainActivity.this, HomeActivity.class);

                    //创建意图 跳转页面 2
                    Intent intent = new Intent(MainActivity.this,HomeActivity.class);

                    //数据传递 1 Bundle
//                    Bundle bundle = new Bundle();
//                    bundle.putString("userName",etUserName.getText().toString());
//                    bundle.putInt("passWold", Integer.parseInt(etPassWord.getText().toString()));
//                    intent.putExtras(bundle);

                    //数据传递2
                    intent.putExtra("userName",etUserName.getText().toString());
                    intent.putExtra("passWold",Integer.valueOf(etPassWord.getText().toString()));
                    //跳转
                    startActivity(intent);
                }else {
                    //密码不正确
                    Toast.makeText(MainActivity.this,"您输入的用户名或密码不正确!",Toast.LENGTH_SHORT).show();
                }


            }
        });

        //重置
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清空
                etUserName.setText("");
                etPassWord.setText("");
            }
        });

        //求和 事件 带返回结果的 意图跳转
        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // mGetContent.launch("1");
                //意图传递参数
                Intent intent = new Intent(MainActivity.this,HomeActivity.class);

                intent.putExtra("numberOne",etNumberOne.getText().toString());
                intent.putExtra("numberTwo",etNumberTwo.getText().toString());

                //跳转 带返回结果
                startActivityForResult(intent,100);
            }
        });


    }

    //返回响应方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
        //响应回来的数据

        textView.setText("requestCode"+requestCode+"resultCode:"+resultCode+"data:"+data.getExtras().getString("sum"));

    }



}

在layout目录下创建一个空白的Empty Views Activity,activity_home.xml




    


    


    

    

会自动创建相对应的Activity类  HomeActivity.java

package com.example.myintent;

import androidx.appcompat.app.AppCompatActivity;

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

public class HomeActivity extends AppCompatActivity {

    private TextView textView,textSum;


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

        textView = findViewById(R.id.btn_home_conten);
        textSum =findViewById(R.id.btn_home_sum);
        //获取意图
        Intent intent = this.getIntent();
        //获取传递过来的参数
        Bundle bundle = intent.getExtras();

        //获取数据
        String userName = bundle.getString("userName");
        int passWold = bundle.getInt("passWold");

//        int numOne  = Integer.valueOf(bundle.getString("numberOne"));
//        int numOne = Integer.valueOf(getIntent().getStringExtra("numberOne"));


        int numOne = Integer.parseInt(getIntent().getStringExtra("numberOne"));
        int numTwo = Integer.parseInt(getIntent().getStringExtra("numberTwo"));


        //设置到textView中
        textView.setText(userName +"----"+ passWold);

       textSum.setText((numOne+numTwo)+"");

        //设置返回 响应码 和 结果
        Intent resIntent = new Intent();
        resIntent.putExtra("sum",(numOne+numTwo)+"");
        setResult(500,resIntent);

    }
}

你可能感兴趣的:(android)