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 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 日本中文字幕在线视频一区 | 精品欧美一区二区在线观看| 国产精品亚洲午夜一区二区三区 | 视频在线一区二区| 无码人妻AV免费一区二区三区| 国模极品一区二区三区| 亚洲天堂一区在线| 亚洲成AV人片一区二区| 中文字幕一区二区三区永久| 色系一区二区三区四区五区| 成人区精品一区二区不卡亚洲| 亚洲综合无码AV一区二区| 国产福利电影一区二区三区,日韩伦理电影在线福 | 亚洲乱色熟女一区二区三区丝袜| 国产在线一区二区杨幂| 无码一区二区三区亚洲人妻| 亚洲av永久无码一区二区三区| 亚洲Av无码一区二区二三区| 久久久精品人妻一区二区三区| 国产在线不卡一区二区三区| 免费一区二区无码东京热| 91一区二区三区| 国产一区二区电影| 日韩免费无码一区二区三区| 亚洲国产成人一区二区三区| 久久一区二区精品| 亚洲综合无码一区二区痴汉| 中日韩精品无码一区二区三区| 亚洲AV日韩综合一区尤物| 无码少妇A片一区二区三区| 欧美日韩国产免费一区二区三区| 麻豆精品人妻一区二区三区蜜桃| 成人区人妻精品一区二区不卡| 韩国一区二区三区视频| 无码精品久久一区二区三区 | 在线观看中文字幕一区| 美女福利视频一区二区| 中文字幕在线播放一区| 国产一区视频在线| 亚洲综合一区二区三区四区五区| 激情久久av一区av二区av三区|