在android里面 Intent 非常重要,它能连接两个Activity。实现页面跳转,学过JAVAWEB的人都知道,有点类似于 Servlet.
在这里有两种方法 传值。
第一种 是批量传送。就是放到Bundle对象中。为了形象的表示,就先写一个登录的界面。 功能就是 点击登录 能把当前的 用户名和密码传到另一个Activity
String usertext = username.getText().toString();//获取文本框内的 内容
String passtext = password.getText().toString();//获取密码框的内容
Intent i = new Intent();//new 一个Intent 对象,用于盛放Bundle
Bundle bundle=new Bundle();//bundle对象 用于把 我们要传的参数,放到Intent里,传送到另一个ACTIVITY
bundle.putString("user",usertext);
bundle.putString("pass", passtext);
i.putExtras(bundle);
i.setClass(LogActivity.this, RestActivity.class);
startActivity(i);
bundle.putString("user",usertext);
bundle.putString("pass", passtext);
至于这两行代码 你可以把它理解为键——值 对,类似于 HaspMap<K,V>.其实它的本质就是HasMap谷歌的官方文档,也是这样写的。
下一步就是在另一个ACTIVITY里获取 我们要传送的值,这里我们把它放到一个 EditText 里面。命名为 textuser和textpass
Bundle bundle=getIntent().getExtras(); String strusername=bundle.getString("user"); String strpass=bundle.getString("pass"); textuser.setText(strusername); textpass.setText(strpass);
这样就可以了第二种 比较简单。可以直接利用intent 传值。
i.putExtra("user", usertext); i.putExtra("pass", passtext);
接受页面:i.setClass(LogActivity.this, RestActivity.class); startActivity(i)
Intent intent = getIntent(); String strusername = intent.getStringExtra("user"); String strpass = intent.getStringExtra("pass");
textuser.setText(strusername); textpass.setText(strpass);
这样就OK了。 接下来还要说一下。当我们把另一个Activity关闭的时候 是可以给上一个 Activity传值的。举个例子,比如我们android自带的音乐播放器。有一个播放列表,当我们点击一首歌曲的时候。这个列表页面就关闭 并把我们点击得歌曲名 返回给播放器,进行播放。接下来我们就实现这个操作。
我们点击关闭按钮当前页面关闭,跳到第一个Activity中。然后Toas
首先我们先要重写onActivityResult这个方法。第二步。我们用intnt发送的值的方法变为@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String result=data.getExtras().getString("result");//获取我们即将要传给这个Activity的值。我们给它取名叫 result Toast.makeText(getBaseContext(), result,Toast.LENGTH_LONG).show();//现实这个我们关闭返回的数据 }
startActivityForResult(i, 1);
第三步,在我们要关闭的页面发送 键——值。
OK。Button btnclose=(Button) findViewById(R.id.btnvlose); btnclose.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("result","点击关闭返回的数据"); RestActivity.this.setResult(RESULT_OK, intent); RestActivity.this.finish(); } });
下面给出本片文章源码。
LogActivity 第一个页面
package com.hr.csdn; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LogActivity extends Activity { private Button denglubtn = null; private Button reset = null; private EditText username = null; private EditText password = null; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String result=data.getExtras().getString("result"); Toast.makeText(getBaseContext(), result,Toast.LENGTH_LONG).show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.pass); denglubtn = (Button) findViewById(R.id.dengbtn); reset = (Button) findViewById(R.id.reset); denglubtn.setOnClickListener(new denglu()); } class denglu implements OnClickListener { public void onClick(View v) { String usertext = username.getText().toString(); String passtext = password.getText().toString(); Intent i = new Intent(); Bundle bundle=new Bundle(); bundle.putString("user",usertext); bundle.putString("pass", passtext); i.putExtras(bundle); /*i.putExtra("user", usertext); i.putExtra("pass", passtext);*/ i.setClass(LogActivity.this, RestActivity.class); //startActivity(i); startActivityForResult(i, 1); } } }
LogActivity 布局的XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/log" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" /> <EditText android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/pass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/dengbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/log" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reset" /> </LinearLayout> </LinearLayout>
第二个Activity页面 JAVA代码RestActivity
package com.hr.csdn; import com.hr.csdn.LogActivity.denglu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class RestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.res); TextView textuser = (TextView) findViewById(R.id.resusername); TextView textpass = (TextView) findViewById(R.id.respass); /*Intent intent = getIntent(); String strusername = intent.getStringExtra("user"); String strpass = intent.getStringExtra("pass");*/ Bundle bundle=getIntent().getExtras(); String strusername=bundle.getString("user"); String strpass=bundle.getString("pass"); textuser.setText(strusername); textpass.setText(strpass); Button btnclose=(Button) findViewById(R.id.btnvlose); btnclose.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("result","点击关闭返回的数据"); RestActivity.this.setResult(RESULT_OK, intent); RestActivity.this.finish(); } }); } }
RestActivity 的布局文件XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/resusername" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/respass" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btnvlose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/close" /> </LinearLayout>
注册文件:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hr.csdn" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LogActivity" 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=".RestActivity" android:label="@string/app_name" ></activity> </application> </manifest>