Android学习(2)Intent_意图

Android学习(2)Intent_意图

Intent概念:

  • 负责对应用中的一次操作的动作、动作涉及的数据、附加数据进行描述。主要用于解决Android应用的各项组件之间的通信,起到一个媒介的作用。

Intent的作用:

  • Intent可以显示Activity,可以发送广播和启动服务
  • 在同一个应用程序中往往会使用Intent对象来指定一个Activity,并通过startActivity或startActivityForResult方法启动一个Activity。
  • 通过Intent在不同的Activity中传递数据

1.已有MainActivity,在xml中给它添加一个按钮,实现点击按钮由MainActicity跳转到TestActivity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    tools:context="com.example.test.androidtest.MainActivity">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test_Activity"
            android:id="@+id/btnTest"/>
</LinearLayout>

这里写图片描述
MainActicity

public class MainActivity extends AppCompatActivity {
    private Button btnTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定组件
        btnTest = (Button)findViewById(R.id.btnTest);
        //设置监听
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //由MainActicity跳转到TestActivity
                intent.setClass(MainActivity.this,TestActivity.class);
                //开始跳转
                startActivity(intent);
            }
        });
  }
}

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.test.androidtest.TestActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换成功"/>
</android.support.constraint.ConstraintLayout>

这里写图片描述
TestActivity

public class TestActivity extends AppCompatActivity {

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

2.实现点击按钮由MainActicity跳转到IntentActivity,并传递参数

在activity_main.xml中添加一个按钮

<Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Intent_Activity"
            android:id="@+id/btnIntent"/>

这里写图片描述
MainActivity

public class MainActivity extends AppCompatActivity {
    private Button btnIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定组件
        btnIntent = (Button)findViewById(R.id.btnIntent);
        //设置监听
                 btnIntent.setOnClickListener(new
                  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //由MainActicity跳转到TestActivity
                intent.setClass(MainActivity.this,IntentActivity.class);
                //设置传递的参数,参数为键值对:("KEY","VALUE")
                intent.putExtra("name","Intent要传递的参数");
                //开始跳转
                startActivity(intent);
            }
        });
  }
}

activity_intent.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.test.androidtest.IntentActivity">

    <TextView
        android:id="@+id/tvIntent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

IntentActivity

public class IntentActivity extends AppCompatActivity {

    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        //绑定组件
        tv = (TextView)findViewById(R.id.tvIntent);
        //接收Intent传递的参数,通过键(KEY)来获取对应的值(VALUE)
        String val = getIntent().getStringExtra("name");
        //设置TextView显示的类容
        tv.setText(val);
    }
}

点击按钮后跳转页面并传递了参数

这里写图片描述
声明:

  1. 知识点来源于《网易云课堂》——《Android基础视频教程》
  2. 本文只用于本人自身学习记录,如有侵权,请立即通知我更改或删除

猜你喜欢

转载自blog.csdn.net/qq_40740256/article/details/82459881