Android: 用Intenti实现Activity之间数据传递
程序功能:使用Intent传递数据、跳转Activity。两个Activity组件,Activity1中包含一个TextView和一个Button。当点击Button时,可以启动Activity2对当前TextView中的内容进行编辑。Activity2的布局界面除了编辑TextView文本信息的编辑框外,还包含两个按钮“保存”和“放弃”。当单击“保存”,Activity2中编辑的文本将返回Activity1,并更新到Activity1中的TextView中;单击“放弃”也将返回Activity1,但之前的编辑操作将被放弃。MainActivity.java[java]package org.hq.testIntent;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {private String msg = "";private EditText editText = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText)super.findViewById(R.id.edittext);editText.setEnabled(false); //设置编辑框不可编辑Button send_btn = (Button)super.findViewById(R.id.send_btn);send_btn.setOnClickListener( (OnClickListener) new OnClickListenerImpl() );}private class OnClickListenerImpl implements OnClickListener //实现OnClickListener类{@Overridepublic void onClick(View v){Intent intent = new Intent();msg = editText.getText().toString(); //获取编辑框中字符串intent.putExtra("org.hq.testIntent.msg", msg); //向下一个Activity发送编辑框中的字符串,初始化时字符串为空intent.setClass(MainActivity.this, EditActivity.class);startActivityForResult(intent,100);//启动下一个Activity,设置一个请求值100}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overrideprotected void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode, resultCode, data);if(requestCode==100) //如果一个返回的Activity的请求值为100,说明是本Activity是调用者{EditText tv = (EditText)findViewById(R.id.edittext);String msg = "";if ( 20 == resultCode ) //如果子Activity返回值为20,说明点击了保存按钮{msg = data.getStringExtra("org.hq.testIntent.msg"); //获取字符串tv.setText(msg); //更改编辑框的内容}}}}package org.hq.testIntent;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {private String msg = "";private EditText editText = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText)super.findViewById(R.id.edittext);editText.setEnabled(false); //设置编辑框不可编辑Button send_btn = (Button)super.findViewById(R.id.send_btn);send_btn.setOnClickListener( (OnClickListener) new OnClickListenerImpl() );}private class OnClickListenerImpl implements OnClickListener //实现OnClickListener类{@Overridepublic void onClick(View v){Intent intent = new Intent();msg = editText.getText().toSt补充:移动开发 , Android ,
上一个:android 图片处理之制作圆角图片
下一个:android打开系统程序