Android系列之Intent与Activity实验

本次实验的目的是了解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);
    }

}

布局文件:
"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" >

    "@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="请输入用户名" />

    "@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:ems="10" >

        
    

    "@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="请输入密码" />

    "@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:ems="10"
        android:inputType="textPassword" />
    "match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
         

Android系列之Intent与Activity实验_第1张图片

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;
        }

    }
}

你可能感兴趣的:(Android,相关知识,计算机专业的相关课程知识)