1.2Activity返回值

Posted Liang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于1.2Activity返回值的主要内容,如果未能解决你的问题,请参考以下文章

(导航组件)返回首页fragment时如何在activity上显示返回箭头?

使用片段返回堆栈处理 ActionBar 标题?

android activity的常用代码:关闭传值返回值回调网页地图短信电话

从另一个 Activity 刷新 Recyclerview

在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始

片段中未调用 onActivityResult