安卓返回信息方式

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"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

 


复制代码

MainActivity

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("T4TAG","按钮的点击监听被触发");
        //静态方法
        //直接用类名就可以调用,不需要实例化
        //构建了一个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,Main2Activity.class);
        //存储内容
        //getExtra Bundle 实际是一个HashMap  进行了限制
        //intent.getExtras().putString("myet",string);
        intent.putExtra("myet",string);

        startActivity(intent);
    }
    //带返回的方式

    public void onCLICK(View v)
    {
        EditText myet=(EditText)findViewById(R.id.myet);

        String string= myet.getText().toString();

        Intent intent= new Intent(this,Main2Activity.class);
        //存储内容
        //getExtra Bundle 实际是一个HashMap  进行了限制
        //intent.getExtras().putString("myet",string);
        intent.putExtra("myet",string);
        //有返回数据的启动方式
        //第一个参数  intent
        //第二个参数  requestCode  请求码
        startActivityForResult(intent, 1);
    }
    //重写   处理返回信息的监听(回调方法)
    //onActivityResult通用监听  监听所有返回信息的
    //必须要有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">


    <EditText
        android:layout_width="100dp"
        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="onCLICK"
        />
</LinearLayout>

 

Main2Activity

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 Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //接受信息
        //获取意图
        //传递过来的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 ONclock(View v)
    {
        //存储返回数据   也要用intent
        EditText mytv=(EditText)findViewById(R.id.mytv);
        Bundle bundle =new Bundle();
        bundle.putString("mytv",mytv.getText().toString());

        //设置返回数据
        // 先设置ReaultCode,再设置存储数据的意图
        setResult(RESULT_OK,new Intent().putExtra("mytv",mytv.getText().toString()));
        //关闭当前activity
        finish();
    }
}

 

activity_main2.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.Main2Activity">


    <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="ONclock"
        />
</LinearLayout>

 

posted @ 2016-03-22 08:43  机智的实习生  阅读(240)  评论(0编辑  收藏  举报