您的位置:首页 > 移动开发 > Android开发

android值传递

2015-07-10 07:49 579 查看
1.值传递就是A界面的数据传到B界面。

<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android