android值传递
2015-07-10 07:49
579 查看
1.值传递就是A界面的数据传到B界面。
界面显示:
2.编写MainActivity.java
(1)从TextEdit中输入一个值
(2)程序中自带的一些数据
(3)通过点击按钮将数据传递到下一个activity
(4)Intent中有很多的putExtra重载的方法,可以放入String,int,Bundle等等
(5)Intent中有很多的getXXXExtra重载的方法,可以获取String,int,Bundle等值
(6)最后调用startActivity(intent);
3.创建HomeActivity.java类
(1)homeActivity接收MainActivity传递的数据
(2)首先要设置homeActivity的布局文件setContentView(R.layout.activity_home);
(3)通过findViewById找到对应的控件
(4)Intent intent =getIntent();获取意图
(5)通过intent.getXXXExtra(“key”)方法获取对应的数据; key表示MainActivity传递的key,
(6)Log.i(TAG, “–result->>”+result);将对应的数据打印到LogCat
(7)查看控制台,校验数据
如果没有显示logcat的控制台:
window—->show View—–>other 输入log
在 Filter过滤的地方输入“–”来过滤我们想要的结果
3.编写activity_home.xml
4.不要忘记在AndroidManifest.xml里面注册HomeActivity
homeActivity界面的显示效果:
下载地址:
http://download.csdn.net/detail/dczjzz/8885757
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.intent.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/ll1" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="intent传值" android:layout_below="@id/ll1" /> </RelativeLayout>
界面显示:
2.编写MainActivity.java
(1)从TextEdit中输入一个值
(2)程序中自带的一些数据
(3)通过点击按钮将数据传递到下一个activity
(4)Intent中有很多的putExtra重载的方法,可以放入String,int,Bundle等等
(5)Intent中有很多的getXXXExtra重载的方法,可以获取String,int,Bundle等值
(6)最后调用startActivity(intent);
package com.example.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button button; private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn); et = (EditText) findViewById(R.id.et); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String result = et.getText().toString().trim(); Intent intent = new Intent(); intent.setClass(MainActivity.this, HomeActivity.class); intent.putExtra("result", result); intent.putExtra("name", "china"); intent.putExtra("age", 25); intent.putExtra("address", "上海"); Bundle bundle = new Bundle(); bundle.putString("code", "1111"); intent.putExtra("bundle", bundle); startActivity(intent); } }); } }
3.创建HomeActivity.java类
(1)homeActivity接收MainActivity传递的数据
(2)首先要设置homeActivity的布局文件setContentView(R.layout.activity_home);
(3)通过findViewById找到对应的控件
(4)Intent intent =getIntent();获取意图
(5)通过intent.getXXXExtra(“key”)方法获取对应的数据; key表示MainActivity传递的key,
(6)Log.i(TAG, “–result->>”+result);将对应的数据打印到LogCat
(7)查看控制台,校验数据
package com.example.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.EditText; public class HomeActivity extends Activity { private EditText et; private final String TAG = "HomeActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); et = (EditText) this.findViewById(R.id.et_home); Intent intent =getIntent(); String result = intent.getStringExtra("result"); Log.i(TAG, "--result->>"+result); String name = intent.getStringExtra("name"); Log.i(TAG, "--name->>"+name); int age = intent.getIntExtra("age",0); Log.i(TAG, "--age->>"+age); String address = intent.getStringExtra("address"); Log.i(TAG, "--address->>"+address); Bundle bundle = intent.getBundleExtra("bundle"); String code = bundle.getString("code"); Log.i(TAG, "--code->>"+code); et.setText(result + " "+name+" "+age+" "+address+" "+code); } }
如果没有显示logcat的控制台:
window—->show View—–>other 输入log
在 Filter过滤的地方输入“–”来过滤我们想要的结果
3.编写activity_home.xml
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.intent.MainActivity" > <EditText android:id="@+id/et_home" android:text="啦啦啦" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_home" /> </RelativeLayout>
4.不要忘记在AndroidManifest.xml里面注册HomeActivity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.intent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".HomeActivity"></activity> </application> </manifest>
homeActivity界面的显示效果:
下载地址:
http://download.csdn.net/detail/dczjzz/8885757
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories