设为首页 加入收藏

TOP

Android 组件系列-----Activity的传值和回传值(三)
2015-08-31 21:23:39 来源: 作者: 【 】 浏览:78
Tags:Android 组件 系列 -----Activity
(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ?
? ? ? ? button = (Button)findViewById(R.id.button);
? ? ? ? editText1 = (EditText)findViewById(R.id.editText1);
? ? ? ? editText2 = (EditText)findViewById(R.id.editText2);
? ? ? ? editText3 = (EditText)findViewById(R.id.editText3);
? ? ? ?
? ? ? ? button.setOnClickListener(new OnClickListener()
? ? ? ? {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? intent.putExtra("message", editText1.getText().toString().trim() + " + " + editText2.getText().toString().trim() + " = ?");
? ? ? ? ? ? ? ? intent.setClass(MainActivity.this, SecondActivity.class);
? ? ? ? ? ? ? ? /*
? ? ? ? ? ? ? ? * 如果希望启动另一个Activity,并且希望有返回值,则需要使用startActivityForResult这个方法,
? ? ? ? ? ? ? ? * 第一个参数是Intent对象,第二个参数是一个requestCode值,如果有多个按钮都要启动Activity,则requestCode标志着每个按钮所启动的Activity
? ? ? ? ? ? ? ? */
? ? ? ? ? ? ? ? startActivityForResult(intent, 1000);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ?
? ? /**
? ? * 所有的Activity对象的返回值都是由这个方法来接收
? ? * requestCode:? ? 表示的是启动一个Activity时传过去的requestCode值
? ? * resultCode:表示的是启动后的Activity回传值时的resultCode值
? ? * data:表示的是启动后的Activity回传过来的Intent对象
? ? */
? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, Intent data)
? ? {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? if(requestCode == 1000 && resultCode == 1001)
? ? ? ? {
? ? ? ? ? ? String result_value = data.getStringExtra("result");
? ? ? ? ? ? editText3.setText(result_value);
? ? ? ? }
? ? }


? ? @Override
? ? public boolean onCreateOptionsMenu(Menu menu)
? ? {
? ? ? ? getMenuInflater().inflate(R.menu.main, menu);
? ? ? ? return true;
? ? }


}


我们看到,这里我们使用的是 startActivityForResult 的这个方法,


public void startActivityForResult (Intent intent, int requestCode)


Same as calling startActivityForResult(Intent, int, Bundle) with no options.


Parameters
intent? ? The intent to start.
requestCode? ? If >= 0, this code will be returned in onActivityResult() when the activity exits.


Throws   android.content.ActivityNotFoundException


第一个指定我们的Intent对象,第二个requestCode指定我们的一个启动标志值,因为我们可能有多个按钮,如果都是跳转到同一个Activity对象上,我们需要对其进行标志,才知道是哪个Activity对象跳转过来的。


我们看到这里还有一个 onActivityResult 方法,这个方法就是用来处理我们Activity的回传值的方法,所有的Activity回传值的操作都是在这个方法中完成。


protected void onActivityResult (int requestCode, int resultCode, Intent data)


Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.


You will receive this call immediately before onResume() when your activity is re-starting.


Parameters
requestCode? ? The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
resultCode? ? The integer result code returned by the child activity through its setResult().
data? ? An Intent, which can return result data to the caller (various data can be attached to Intent "extras").


接下来,我们看看SecondActivity这个类:


public class SecondActivity extends Activity
{
? ? private Button button;
? ? private TextView textView;
? ? private EditText editText;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState)
? ? {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.second);
? ? ? ?
? ? ? ? button = (Button)findViewById(R.id.button1);
? ? ? ?

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Shell脚本:批量添加用户,并设置.. 下一篇Android 组件系列-----Activity保..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: