-->
当前位置:首页 > 题库 > 正文内容

移动应用开发 实验报告4 Intent的使用

Luz4年前 (2021-08-08)题库1449

一、实验目的

掌握界面跳转的方法;

使用Intent实现不同activity之间的数据传输。

二、实验内容

使用Intent实现不同activity之间的数据传输。

Luz页面的按钮实现到Luz2的跳转,并将文本传输到Luz2

三、实验原理

通过Data只能传输比较简单的数据,Intent主要通过Extra完成组件之间的数据传递。

 

调用方通过intent的putExtra设置数据,被调用的组件通过getXXXExtra获取数据


 

四、实验代码及结果

代码:

package Luz.Luzniubi;

import   android.app.Activity;

import   android.content.Intent;

import   android.net.Uri;

import android.os.Bundle;

import   android.view.View;

import   android.view.View.OnClickListener;

import   android.widget.Button;

public class Luz   extends Activity {

private Button myButton = null;

    @Override

    public void onCreate(Bundle   savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        myButton =   (Button)findViewById(R.id.myButton);

        myButton.setOnClickListener(new   MyButtonListener());

    }

    class MyButtonListener implements   OnClickListener{

    @Override

    public void onClick(View v) {

        Intent intent = new Intent();

        intent.putExtra("testIntent",   "www.hyluz.cn");

        intent.setClass(Luz.this,   OtherActivity.class);

        Luz.this.startActivity(intent);

 

    }

     

    }

}

 

package Luz.Luzniubi;

 

import   android.app.Activity;

import   android.content.Intent;

import   android.os.Bundle;

import   android.widget.TextView;

 

public class   OtherActivity extends Activity{

private TextView myTextView = null;

@Override

protected void onCreate(Bundle   savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.other);

    Intent intent = getIntent();

    String value =   intent.getStringExtra("testIntent");

    myTextView = (TextView)findViewById(R.id.myTextView);

    myTextView.setText(value);

   

}

 

}

 

<?xml version="1.0"   encoding="utf-8"?>

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<Button

    android:id="@+id/myButton"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="你传你马呢"

    />

</LinearLayout>

 

 

<?xml version="1.0"   encoding="utf-8"?>

<!-- 该文件是OtherActivity.java文件所使用的布局文件 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView

    android:id="@+id/myTextView"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    />

</LinearLayout>

 

<?xml version="1.0"   encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="Luz.Luzniubi"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".Luz"

                  android:label="Luz">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"   />

            </intent-filter>

        </activity>

       <activity android:name=".OtherActivity" android:label="Luz2"/>

    </application>

    <uses-sdk android:minSdkVersion="4" />

 

</manifest>

 

 

 

 image.png


image.png


 

 

 

 


 

 

 

 

 

 


发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。