设为首页 加入收藏

TOP

Android 组件系列-----Activity的传值和回传值(四)
2015-08-31 21:23:39 来源: 作者: 【 】 浏览:77
Tags:Android 组件 系列 -----Activity
textView = (TextView)findViewById(R.id.textView1);
? ? ? ? editText = (EditText)findViewById(R.id.editText1);
? ? ? ?
? ? ? ? Intent intent = getIntent();
? ? ? ? String message = intent.getStringExtra("message").toString().trim();
? ? ? ? textView.setText(message);
? ? ? ?
? ? ? ? button.setOnClickListener(new OnClickListener()
? ? ? ? {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? String result = editText.getText().toString().trim();
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? intent.putExtra("result", result);
? ? ? ? ? ? ? ? /*
? ? ? ? ? ? ? ? * 调用setResult方法表示我将Intent对象返回给之前的那个Activity,这样就可以在onActivityResult方法中得到Intent对象,
? ? ? ? ? ? ? ? */
? ? ? ? ? ? ? ? setResult(1001, intent);
? ? ? ? ? ? ? ? //? ? 结束当前这个Activity对象的生命
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}


这个类里面我们将MainActivity传过来的值给TextView赋上,然后通过点击按钮将值回传给我们的 MainActivity,通过 setResult 方法可以将我们的值保存在Intent对象中,并返回给MainActivity对象,通过的 onActivityResult 方法来处理得到的回传Intent对象。finish()方法我们可以显示的结束这个Activity对象的生命。


最后我们来看一下AndroidManifest.xml文件:



? ? package="com.xiaoluo.android_activity"
? ? android:versionCode="1"
? ? android:versionName="1.0" >


? ? ? ? ? ? android:minSdkVersion="8"
? ? ? ? android:targetSdkVersion="18" />


? ? ? ? ? ? android:allowBackup="true"
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:label="@string/app_name"
? ? ? ? android:theme="@style/AppTheme" >
? ? ? ? ? ? ? ? ? ? android:name="com.xiaoluo.android_activity.MainActivity"
? ? ? ? ? ? android:label="@string/app_name" >
? ? ? ? ? ?
? ? ? ? ? ? ? ?


? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ?
? ? ? ? ? ? ? ? ? ? android:name="com.xiaoluo.android_activity.SecondActivity"
? ? ? ? ? ? android:label="SecondActivity" >
? ? ? ?



我们来看看效果图:





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

评论

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