【Android】Activity跳轉傳值實例

題目

如圖爲四個彩色的按鈕,當點擊某一顏色的按鈕時,跳轉頁面顯示該按鈕對應的顏色。
在這裏插入圖片描述
html

實現

src/main/AndroidManifest.xmljava

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ShowActivity">

        </activity>
    </application>

</manifest>

com/example/test/MainActivity.javaandroid

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private Button bt1,bt2,bt3,bt4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1=findViewById(R.id.btn1);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                intent.putExtra("data", "red");
                startActivity(intent);
            }
        });


        bt2=findViewById(R.id.btn2);

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                intent.putExtra("data", "green");
                startActivity(intent);
            }
        });

        bt3=findViewById(R.id.btn3);

        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                intent.putExtra("data", "blue");
                startActivity(intent);
            }
        });


        bt4=findViewById(R.id.btn4);

        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                intent.putExtra("data", "yellow");
                startActivity(intent);
            }
        });
    }

}

com/example/test/ShowActivity.javaapp

package com.example.test;

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

public class ShowActivity extends Activity {
    private TextView mText;

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

        mText = (TextView) findViewById(R.id.text1);

        Intent intent = getIntent();

        String message = intent.getStringExtra("data");

        mText.setText(message);

    }
}

layout/activity_main.xmlide

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal">
        <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" />
        <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1">
        <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="1" />
        <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#ffff00" android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

layout/activity_show.xmlthis

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="100dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

相關文章
相關標籤/搜索