• 旺仔闲来无事,练习Intent的带返回信息的打开方式


    package com.hanqi.myintent;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import javax.xml.transform.Result;
    
    public class TestIntent extends AppCompatActivity {
        EditText et_1;
        String name,name1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_intent);
            et_1=(EditText)findViewById(R.id.et_1);
        }
        public void bt_OnClick(View v)
        {
            Intent intent=new Intent(TestIntent.this,TestIntent1.class);
            startActivityForResult(intent,1);
        }
        public void bt1_OnClick(View v)
        {
            name1=et_1.getText().toString();
            if (name1.equals(name))
            {
                Toast.makeText(TestIntent.this, "返回信息成功", Toast.LENGTH_SHORT).show();
            }
        }
        public void bt2_OnClick(View v)
        {
            Intent intent1=new Intent(Intent.ACTION_CALL);
            Uri uri=Uri.parse("tel:"+110);
            intent1.setData(uri);
            try{
            startActivity(intent1);
            }catch (Exception e){}
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==1)
            {
                if (resultCode==RESULT_OK)
                {
                   name=data.getStringExtra("name");
                }
            }
        }
    }
    package com.hanqi.myintent;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    
    public class TestIntent1 extends AppCompatActivity {
    EditText et_1;
        String name;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_intent1);
            et_1=(EditText)findViewById(R.id.et_1);
        }
        public void bt_OnClick(View v)
        {
            name=et_1.getText().toString();
            Intent intent=new Intent();
            intent.putExtra("name",name);
            setResult(RESULT_OK, intent);
            finish();
        }
        public void bt1_OnClick(View v)
        {
            setResult(RESULT_CANCELED,null);
            finish();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        tools:context="com.hanqi.myintent.TestIntent"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入信息"
            android:id="@+id/et_1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="带返回的打开方式"
            android:onClick="bt_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="返回信息确认"
            android:onClick="bt1_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="拨打电话"
            android:onClick="bt2_OnClick"/>
    
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        tools:context="com.hanqi.myintent.TestIntent1"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入信息"
            android:id="@+id/et_1"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="返回"
            android:onClick="bt_OnClick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消"
            android:onClick="bt1_OnClick"/>
    
    </LinearLayout>
  • 相关阅读:
    Android之打包签名
    on a null object reference 问题的解决办法
    Android Fragment使用小结及介绍
    Android开发重点难点:RelativeLayout(相对布局)详解
    Android的学习第六章(布局一LinearLayout)
    与adb相关的问题,比如掉线问题、Android Studio 提示Session 'app':Error Installing APK、找不到设备
    Android LitePal介绍与使用说明
    学习进度条-10
    《梦断代码》阅读笔记03
    用户模板和用户场景
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5680272.html
Copyright © 2020-2023  润新知