Android顯示Intent示例含代碼

Android顯示Intent示例

Android顯示intent 指定要從活動調用的組件。愛掏網 - it200.com換句話說,我們可以通過顯示intent在Android中調用另一個活動。愛掏網 - it200.com

我們還可以使用顯示intent將信息從一個活動傳遞到另一個活動。愛掏網 - it200.com

在這里,我們將看到一個示例,調用一個活動從另一個活動,并且反過來也是如此。愛掏網 - it200.com

Android從一個活動調用另一個活動的示例

讓我們來看一個簡單的示例,Android顯示用例從另一個活動調用一個活動,反之亦然。愛掏網 - it200.com

activity_main.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="example.javatpoint.com.explicitintent.FirstActivity">  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        android:text="First Activity"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.454"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.06" />  

    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="392dp"  
        android:onClick="callSecondActivity"  
        android:text="Call second activity"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  

</android.support.constraint.ConstraintLayout>  

ActivityOne 類

package example.javatpoint.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
    }
    public void callSecondActivity(View view){
        Intent i = new Intent(getApplicationContext(), SecondActivity.class);
        i.putExtra("Value1", "Android By Javatpoint");
        i.putExtra("Value2", "Simple Tutorial");
        // Set the request code to any code you like, you can identify the
        // callback via this code
        startActivity(i);
    }

}

activitytwo_main.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="example.javatpoint.com.explicitintent.SecondActivity">  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        android:text="Second Activity"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.454"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.06" />  

    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="392dp"  
        android:onClick="callFirstActivity"  
        android:text="Call first activity"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  
</android.support.constraint.ConstraintLayout>  

ActivityTwo類

package example.javatpoint.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Bundle extras = getIntent().getExtras();
        String value1 = extras.getString("Value1");
        String value2 = extras.getString("Value2");
        Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+
                "\n Second Value: "+value2, Toast.LENGTH_LONG).show();
    }
    public void callFirstActivity(View view){
        Intent i = new Intent(getApplicationContext(), FirstActivity.class);
        startActivity(i);
    }

}

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 67pao强力打造67194在线午夜亚洲| 一本色综合久久| 猫咪www免费人成网站| 日韩日韩日韩日韩日韩| 四虎影8818| 99在线视频精品费观看视| 曰本女同互慰高清在线观看| 国产无套内射久久久国产| 中文字幕在线2021| 欧美污视频网站| 国产∨亚洲v天堂无码久久久| 992tv成人影院| 日本三级在线观看免费| 亚洲精品欧美精品日韩精品| 韩国无码av片| 在线观看国产亚洲| 久久人人爽人人爽人人片AV东京热 | 本道久久综合88全国最大色| 又色又污又黄无遮挡的免费视| 3d动漫精品啪啪一区二区中文| 欧美成人免费全部| 国产A√精品区二区三区四区| 538在线精品| 成人精品一区二区三区中文字幕| 亚洲天堂一区二区三区| 网站在线观看你懂的| 国产精品久久久久久久久齐齐 | 毛片视频网站在线观看| 国产三级放荡的护士| 3d性欧美动漫精品xxxx| 成人免费v片在线观看| 亚洲a级片在线观看| 男女啪啪免费体验区| 国产人成在线视频| 一区二区三区视频免费观看| 最近最新中文字幕完整版免费高清 | 西西人体欧美大胆在线| 很黄很污的视频网站| 久久精品国产亚洲一区二区| 波多野结衣33| 又黄又粗又爽免费观看|