Android系列之Intent与Activity实验

来源:互联网 发布:网络个人信息安全问题 编辑:程序博客网 时间:2024/05/22 11:18

本次实验的目的是了解Intent的Action、Catalog、Data等属性的用法,掌握Intent的启动机制以及利用Intent意图在应用程序Activity间启动、停止和传输数据。

题目一在实验1建立的登录界面基础上,实现:点击登录按钮后,显示登录成功界面,并显示登录用户名,即:“欢迎XXX使用本系统字样”;Activity:package com.example.project_itak;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{    Button bt;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bt = (Button) findViewById(R.id.login);        bt.setOnClickListener(this);    }    public void onClick(View v) {        Intent intent = new Intent();        intent.setClass(MainActivity.this, MainActivity2.class);        Bundle bundle = new Bundle();        bundle.putString("username", "zhangjiangtao");        intent.putExtras(bundle);        startActivity(intent);    }}布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    tools:context="com.example.ex4_15linearlayoutloginwindows.MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入用户名" />    <EditText        android:id="@+id/username"        android:layout_width="wrap_content"        android:layout_height="wrap_content"          android:ems="10" >        <requestFocus />    </EditText>    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入密码" />    <EditText        android:id="@+id/password"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="textPassword" />    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">         <Button        android:id="@+id/login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登录" />         <Button        android:id="@+id/cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="取消" />    </LinearLayout></LinearLayout>Activity2:package com.example.project_itak;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;public class MainActivity2 extends Activity {    protected void onCreate(Bundle savedInstanceState) {        TextView tv;        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main_activity2);        Intent intent = this.getIntent();        tv = (TextView)findViewById(R.id.tv);        Bundle bundle = intent.getExtras();        String username = bundle.getString("username");        String ans = "欢迎"+username+"来到本系统";        tv.setText(ans);    }}布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.project_itak.MainActivity2" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text=""/></RelativeLayout>题目二:使用系统提供的Intent Action,完成如下图所示的功能。

这里写图片描述

package com.example.project3;import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{    Button brower,window,call,messege,music,search;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        brower = (Button)findViewById(R.id.brower);        call = (Button)findViewById(R.id.call);        messege = (Button)findViewById(R.id.messege);        window = (Button)findViewById(R.id.window);        music = (Button)findViewById(R.id.music);        search = (Button)findViewById(R.id.search);        brower.setOnClickListener(this);        call.setOnClickListener(this);        messege.setOnClickListener(this);        window.setOnClickListener(this);        music.setOnClickListener(this);        search.setOnClickListener(this);    }    //Intent intent = new Intent();    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {            case R.id.brower:                String link = "http://www.baidu.com";                Uri uri = Uri.parse(link);                Intent intent1 = new Intent(Intent.ACTION_VIEW,uri);                startActivity(intent1);                break;            case R.id.window:                String phone = "tel:1361234";                Intent intent2 = new Intent(Intent.ACTION_DIAL,Uri.parse(phone));                startActivity(intent2);                break;            case R.id.call:                String call = "tel:+1361234";                Intent intent3 = new Intent(Intent.ACTION_CALL,Uri.parse(call));                startActivity(intent3);                break;            case R.id.messege:                String mess = "smsto:1361234";                Intent intent4 = new Intent(Intent.ACTION_SENDTO,Uri.parse(mess));                intent4.putExtra("sms_body", "ni tai shuai la ");                startActivity(intent4);                break;            case R.id.music:                String music="file:///sdcard/redbean.mp3";                Intent intent5 = new Intent(Intent.ACTION_VIEW);                intent5.setDataAndType(Uri.parse(music),"audio/mp3");                startActivity(intent5);                break;            case R.id.search:                String se = "android程序设计";                Intent intent6 = new Intent();                intent6.setAction(Intent.ACTION_WEB_SEARCH);                intent6.putExtra(SearchManager.QUERY, se);                startActivity(intent6);                break;        }    }}
0 0
原创粉丝点击