Android课程---Activity 带返回值的跳转

时间:2023-03-08 18:43:09

Activity2.java

package com.hanqi.test4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText; public class Activity2 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2); //接收信息
//获取意图 //传递过来的Intent
Intent intent = getIntent(); String s = intent.getExtras().getString("myet");
EditText mytv = (EditText)findViewById(R.id.mytv);
mytv.setText(s);
}
//普通返回
public void onclick(View v)
{
//关闭当前activity
finish();
}
public void onclick2(View v)
{
//存储返回数据 也要用intent EditText mytv = (EditText)findViewById(R.id.mytv);
// Log.e("TAG", "mytv =" + mytv.getText().toString());
//
Bundle bundle = new Bundle();
bundle.putString("mytv", mytv.getText().toString());
//设置返回数据
//先设置ResultCode,再设置存储数据的意图
setResult(RESULT_OK, new Intent().putExtras(bundle)); finish(); }
}

activity_2.xml

<?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.test4.Activity2"> <EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="测试"
android:id="@+id/mytv"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通返回"
android:onClick="onclick"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带数据返回"
android:onClick="onclick2"
/> </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.test4"> <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"
android:label="@string/app_name">
<intent-filter> <!--//意图过滤器-->
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".Activity2"></activity>
</application> </manifest>

MainActivity.java

package com.hanqi.test4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; /**
* Created by Administrator on 2016/3/21.
*/
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main_layout);
} //普通方式
public void onClick(View v)
{
Log.e("Test4_TAG", "按钮的点击监听被触发");
//静态方法
//特点:不用实例化/直接用类名就可以调用
//构建了Toast实例
//方法链(一个方法后面跟另一个方法)
Toast.makeText(this,"按钮的点击监听被触发",Toast.LENGTH_LONG).show(); // Toast toast = Toast.makeText(this, "按钮的点击被触发", Toast.LENGTH_LONG);
// toast.show();
//意图 intent
//取得要传递的信息
//获取view实例 EditText myet = (EditText)findViewById(R.id.myet);
String string = myet.getText().toString();
Intent intent = new Intent(this,Activity2.class);
//存储内容
//getExtra Bundle 实际是一个HashMap,进行限制
//intent.getExtras().putString("myet",string); intent.putExtra("myet",string);//重载 //启动方法不一样
startActivity(intent); } //带返回的方式 public void onclick2(View v)
{
EditText myet = (EditText)findViewById(R.id.myet);
String string = myet.getText().toString();
Intent intent = new Intent(this,Activity2.class); intent.putExtra("myet", string); //有返回数据的启动方式
//第一个参数是intent
//第二个参数是requestCode(请求码)作用:为了区分不同的请求
startActivityForResult(intent,1); } //重写 处理返回信息的监听(也叫回调方法)
//监听所有返回信息的
//必须要有requestCode区分由哪个请求返回的 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("TAG","requestCode="+requestCode+"resultCode"+resultCode); if(requestCode ==1)
{
if (resultCode ==RESULT_OK)
{
//获取返回信息
String string = data.getExtras().getString("mytv");
EditText editText = (EditText)findViewById(R.id.myet);
editText.setText(string);
Toast.makeText(this, "返回信息 =" + string, Toast.LENGTH_LONG);
}
else
{
Toast.makeText(this,"返回信息有误",Toast.LENGTH_SHORT);
}
}
} }

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/myet"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通方式"
android:onClick="onClick"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带返回方式"
android:onClick="onclick2"/>
</LinearLayout>