• 1.2Activity返回值


    1.意图方法:startActivityForResult---请求码的startActivity

    2.设置返回码和返回值/结束Activity:setResult和finish

    3.接收返回值:重写onActivityResult,判断请求码和返回码

    Manifest.xml:清单文件 

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.manusas.activitycalbak">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            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=".NextActivity">
                <intent-filter>
                    <action android:name="nextaction" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    View Code

    MainActivity.java 

    package com.manusas.activitycalbak;
    
    import android.app.Activity;
    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;
    
    public class MainActivity extends Activity {
        private EditText plus1;
        private EditText plus2;
        private EditText showsumnum;
        private Button btngoto;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            plus1 = (EditText) this.findViewById(R.id.plus1);
            plus2 = (EditText) this.findViewById(R.id.plus2);
            showsumnum = (EditText) this.findViewById(R.id.showsumnum);
            btngoto = (Button) this.findViewById(R.id.btngoto);
            btngoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, NextActivity.class);
                    String p1 = plus1.getText().toString().trim();
                    String p2 = plus2.getText().toString().trim();
                    i.putExtra("p1", p1);
                    i.putExtra("p2", p2);
                    //startActivityForResult 100是请求码 相当于请求pk 不是startActivity了
                    startActivityForResult(i, 100);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            //重写接收返回值方法 判断请求和返回码
            if (requestCode == 100 && resultCode == 200) {
                String sum = data.getStringExtra("sum");
                showsumnum.setText(sum);
            }
        }
    }
    View Code

    NextActivity.java

    package com.manusas.activitycalbak;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class NextActivity extends Activity {
        private TextView showplus;
        private EditText sumnum;
        private Button btncalbak;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next);
            Intent i = getIntent();
            String s = i.getStringExtra("p1") + "+" + i.getStringExtra("p2") + ":";
            showplus = (TextView) this.findViewById(R.id.showplus);
            showplus.setText(s);
            btncalbak = (Button) findViewById(R.id.btncalbak);
            btncalbak.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sumnum = (EditText) findViewById(R.id.sumnum);
                    String sumn = sumnum.getText().toString().trim();
                    Intent calbak = new Intent();
                    calbak.putExtra("sum", sumn);
                    //设置返回值方法 200返回码 相当于返回值pk
                    setResult(200, calbak);
                    finish();
                }
            });
        }
    }
    View Code

     

    activity_main.xml:主页

    <?xml version="1.0" encoding="utf-8"?>
    <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.manusas.activitycalbak.MainActivity">
    
        <EditText
            android:id="@+id/plus1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/textView"
            android:ems="10"
            android:inputType="number" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="加数1:"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/plus1"
            android:text="加数2:"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <EditText
            android:id="@+id/plus2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/plus1"
            android:layout_below="@+id/plus1"
            android:ems="10"
            android:inputType="number" />
    
        <Button
            android:id="@+id/btngoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/plus2"
            android:text="跳转" />
    
        <EditText
            android:id="@+id/showsumnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/btngoto"
            android:layout_centerHorizontal="true" />
    
    </RelativeLayout>
    View Code

     

    activity_next.xml:跳转页

    <?xml version="1.0" encoding="utf-8"?>
    <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.manusas.activitycalbak.MainActivity">
    
        <TextView
            android:id="@+id/showplus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="和:"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <EditText
            android:id="@+id/sumnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/showplus"
            android:ems="10"
            android:inputType="number" />
    
        <Button
            android:id="@+id/btncalbak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/sumnum"
            android:layout_alignParentEnd="true"
            android:text="值回传" />
    </RelativeLayout>
    View Code
  • 相关阅读:
    Asp.net 中 OnClientClick 与 OnClick 的区别
    WPF + RDLC + Tablix动态生成列 + 表头合并
    统计表用于查询效果太差的情况
    TeamView WaitforConnectFailed错误原因
    瞄到BindingGroup用法
    最讨厌工作时候百度的信息
    Lambda获取类属性的名字
    附加属性作用于多选控件,用于获取所有选中项
    文件夹图标错落解决方案
    WPF Line 的颜色过度动画
  • 原文地址:https://www.cnblogs.com/manusas/p/5428879.html
Copyright © 2020-2023  润新知