当一个Android运行后,需要经常的跳转页面(比如查看->返回),那么在跳转的同时也需要携带一些源页面的数据到目的页面。下面就举例说一下此类需求的实现方法
首先创建源页面视图first.xml
<LinearLayout 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"
tools:context=".First" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入一个字符串"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="Btn"
/>
</LinearLayout>
创建源页面类first.class
package com.example.demo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class First extends Activity {
private Button btn = null;
private EditText et = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
//获取视图中资源
btn = (Button)findViewById(R.id.btn);
et = (EditText)findViewById(R.id.et);
//为button添加监听事件(内部类)
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = et.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("name",name);
Intent intent = new Intent(First.this,Second.class);
intent.putExtras(bundle);
//别忘了启动intent
startActivity(intent);
}
});
}
}
创建源页面视图second.xml
<LinearLayout 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"
tools:context=".First" >
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
创建源页面类first.class
package com.example.demo2;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.TextView;
public class Second extends Activity{
private TextView tv= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv = (TextView)findViewById(R.id.result);
Intent intent = getIntent();
Bundle b = intent.getExtras();
String name = b.getString("name");
tv.setText(name);
// String[] projection = new String[] {People._ID,People.NAME,People.NUMBER};
// Uri contacts = People.CONTENT_FILTER_URI;
// String[] args = {name};
// Cursor managedCursor = managedQuery(contacts, projection, "name=?", args, People.NAME+"ASC");
// if(managedCursor.moveToFirst()) {
// String name1 = managedCursor.getString(1);
//
// String number = managedCursor.getString(2);
// tv.setText(name);
// }
}
}