添加Activity并传递数据

来源:互联网 发布:mac可用的魔兽世界插件 编辑:程序博客网 时间:2024/06/08 11:02
第一步:新建一个继承Activity的类,

   //这里可以使用setContentView(R.layout.xxx)显示某个视图....

     }

package com.example.hello;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class ShowActivity extends Activity { private String name; private int age;private TextView tvshow;protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_show);getdata();initview();binddata();}private void binddata() {// TODO Auto-generated method stubtvshow.setText("你好"+name+"年龄是"+age);}private void initview() {// TODO Auto-generated method stubtvshow= (TextView) findViewById(R.id.tvshow);}private void getdata() {// TODO Auto-generated method stub/*Bundle bundle= getIntent().getExtras();  name= bundle.getString("name");  age=bundle.getInt("age");*/name = getIntent().getStringExtra("name");age= getIntent().getIntExtra("age", 12);}}


}
第二步:需要在功能清单AndroidManifest.xml文件中声明新建的activity_show.xml布局文件。
 
 <activity            android:name="com.example.hello.MainActivity"            android:label="@string/app_name" >            <intent-filter>

android:name属性值的前面加了一个点表示ShowActivity是当前包cn.edu.bzu.activity下的类,如果类在应用
的当前包下,可以省略点符号,如果类在应用的子包下必须加点,如:ShowActivity类在

cn.edu.bzu.activity.user包下可以这样写:<activity android:name=“.user.ShowActivity“ />

第三步为intent附加数据的两种写法:

package com.example.hello;import android.R.string;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;import android.widget.Toast;public class MainActivity extends Activity {    private Button bt;    private EditText et;    public static String tag="MainActivity";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bt= (Button) findViewById(R.id.button1);        et=(EditText) findViewById(R.id.editText1);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    public void sayhello(View view)    {    String name =et.getText().toString();    Intent intent = new Intent();intent.setClass(this, ShowActivity.class);/*Bundle bundle  = new Bundle();bundle.putString("name", name);bundle.putInt("age", 16);intent.putExtras(bundle);*/intent.putExtra("name", name);intent.putExtra("age", 12);startActivity(intent);    }    }


第一种写法,用于批量添加数据到Intent:
Intent intent = new Intent();
Bundle bundle = new Bundle();//该类用作携带数据
bundle.putString(“name”, “张三");
intent.putExtras(bundle);//为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换
第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个地添加进Intent,这
种写法使用起来比较方便,而且只需要编写少量的代码。
Intent intent = new Intent();
intent.putExtra(“name”, “赵旭");
Intent提供了各种常用类型重载后的putExtra()方法,如: putExtra(String name, String value)、 
putExtra(String name, long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个
Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra()方法传入的值都会存放于该Bundle
对象,下面是Intent的putExtra(String name, String value)方法代码片断:
public class Intent implements Parcelable {
private Bundle mExtras;
public Intent putExtra(String name, String value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putString(name, value);
        return this;
 }


第四步传递参数:

第二种:打开新的Activity,并传递若干个参数给它:
public class HelloActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
  btnClick.setOnClickListener(new View.OnClickListener(){//点击该按钮会打开一个新的Activity
           public void onClick(View v) {
                            Intent intent = new Intent(HelloActivity.this, ShowActivity.class)
Bundle bundle = new Bundle();//该类用作携带数据
bundle.putString(“name”, “张三");
bundle.putInt("age", 24);
intent.putExtras(bundle);//附带上额外的数据
startActivity(intent);
 }}); }
}
在新的Activity中接收前面Activity传递过来的参数:
public class ShowActivity extends Activity {
           protected void onCreate(Bundle savedInstanceState) {
                           Bundle bundle = this.getIntent().getExtras();
                          String name = bundle.getString("name");
                           int age = bundle.getInt("age");

0 0