Android开发之Intent的简单案例

大神勿喷!这只是我学习移动开发过程中的练习。谢谢
效果图:

在这里插入图片描述
代码如下:
第一个活动的代码:MainActivity

package com.example.program.shiyan1app;

import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText edittext1;
    private EditText  edittext2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstlayout);
        //按钮区
        Button button_1 = (Button) findViewById(R.id.btn1);
        Button button_2 = (Button) findViewById(R.id.btn2);
        Button button_3 = (Button) findViewById(R.id.btn3);
        Button button_4 = (Button) findViewById(R.id.btn4);
        Button button_5 = (Button) findViewById(R.id.btn5);
        Button button_6 = (Button) findViewById(R.id.btn6);
        Button button_7 = (Button) findViewById(R.id.btn7);
        Button button_8 = (Button) findViewById(R.id.btn8);
        Button button_9 = (Button) findViewById(R.id.btn9);

        //文本编辑区
        edittext1 = (EditText) findViewById(R.id.edtext1);
        edittext2 = (EditText) findViewById(R.id.edtext2);


        //按钮功能实现区
        //使用Toast提示
        button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"这是一个Toast提示",Toast.LENGTH_SHORT).show();
            }
        });

        //显式跳转第二个活动
        button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,secondActivity.class);
                startActivity(intent);
            }
        });

        //隐式跳转到第二个活动
        button_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.action.ACTION_START");
                startActivity(intent);
            }
        });

        //调用拨号键
        button_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:1776560"));
                startActivity(intent);
            }
        });


        //调用系统浏览器
        button_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.baidu.com"));
                startActivity(intent);
            }
        });


        //调用录音机
        button_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
                startActivity(intent);

            }
        });

        //发送短信
        button_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (Intent.ACTION_VIEW);
                intent.putExtra("sms_body","The SMS text");
                intent.setType("vnd.android-dir/mms-sms");
                startActivity(intent);
            }
        });

        //向下一个活动传送数据
        button_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,secondActivity.class);
                String data = edittext1.getText().toString();
                intent.putExtra("chuan_data",data);
                startActivity(intent);
            }
        });

        //点击跳转的第二个活动。
        button_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this,secondActivity.class);
                startActivityForResult(intent,1);


            }
        });


    }

    //重写onActivityResult()方法获取第二个活动回传的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch(requestCode){
            case 1:
                if (resultCode==RESULT_OK){
                    String returnedData = data.getStringExtra("activity2_data");
                    edittext2.setText(returnedData);
                }
                break;
            default:
        }
    }
}

第二个活动的代码:secondActivity

package com.example.program.shiyan1app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class secondActivity extends AppCompatActivity {
    private EditText Editext2_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlayout);

        //显示文字区
        TextView view_2 = findViewById(R.id.view2);

        //可编辑文本区
        Editext2_1 = (EditText) findViewById(R.id.edtext2_1);

        //按钮区
        Button button2_1 = (Button) findViewById(R.id.btn2_1);


        //功能实现区
        //接收MainActivity传送过来的数据
        Intent intent = getIntent();
        String data = intent.getStringExtra("chuan_data");
        view_2.setText(data);

        //向上一个活动传递数据
        button2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data  = Editext2_1.getText().toString();
                Intent intent  = new Intent();
                intent.putExtra("activity2_data",data);
                setResult(RESULT_OK,intent);
                finish();
            }
        });


    }
}

AndroidManifest.xml文件中的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.program.shiyan1app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </activity>
        <activity android:name=".secondActivity">
            <intent-filter>
                <action android:name="com.example.action.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>


        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值