标签:
介绍:
<?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="horizontal"> <EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/send_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:onClick="sendMessage" /> <!--定义onclick函数--> </LinearLayout>
package com.example.administrator.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { //发送Intent对应字符串内容的key public static final String Intent_key="MESSAGE"; //EditText private EditText editText =null; private void initView(){ editText= (EditText) findViewById(R.id.edit_message); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置布局 setContentView(R.layout.activity_main); initView(); } //发送消息,启动secondActivity! public void sendMessage(View view){ Intent intent = new Intent(this,secondActivity.class); String text =editText.getText().toString(); intent.putExtra(Intent_key,text); startActivityForResult(intent,0);//此处的requestCode应与下面结果处理函中调用的requestCode一致 } //结果处理函数,当从secondActivity中返回时调用此函数 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0 && resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); String text =null; if(bundle!=null) text=bundle.getString("second"); Log.d("text",text); editText.setText(text); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="50dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="点击按钮返回" /> </LinearLayout>
package com.example.administrator.helloworld; 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.TextView; import android.view.View.OnClickListener; public class secondActivity extends AppCompatActivity { private Button button=null; private TextView textView =null; //设置类ButtonListener实现接口,OnClickListener,在其中可以指定不同id的button对应不同的点击事件 //可以借此将代码抽出来,提高代码的可阅读性 private class ButtonListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: Intent intent =getIntent(); //这里使用bundle绷带来传输数据 Bundle bundle =new Bundle(); //传输的内容仍然是键值对的形式 bundle.putString("second","hello world from secondActivity!");//回发的消息,hello world from secondActivity! intent.putExtras(bundle); setResult(RESULT_OK,intent); finish(); break; } } } //初始化View public void initView(){ button= (Button) findViewById(R.id.button); textView= (TextView) findViewById(R.id.textView); button.setOnClickListener( new ButtonListener() ); Intent intent =getIntent(); String text =intent.getStringExtra(MainActivity.Intent_key); textView.setText(text); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); initView(); } }
标签:
原文地址:http://www.cnblogs.com/zhoudayang/p/5259453.html