Android开发之Intent的简单案例

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

Android开发之Intent的简单案例_第1张图片
代码如下:
第一个活动的代码: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文件中的代码




    
        
            
                
                
            

        
        
            
                
                
            


        
    


你可能感兴趣的:(练习)