android bundle 简单传值

by admin on 2020年1月5日
package com.example.postvalue;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondPage.class);
                Bundle bundle = new Bundle();
                bundle.putString("用户名:", "张三");
                bundle.putString("密码:", "888888");
                intent.putExtras(bundle);
                MainActivity.this.startActivity(intent);
            }

        });

        Toast.makeText(this, "准备发送!?", 3000).show();

    }

    @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;
    }

}
/*******************************/
package com.example.postvalue;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

public class SecondPage extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle bunde = this.getIntent().getExtras();
        String eml = bunde.getString("用户名:");  
        String pwd = bunde.getString("密码:");    

        Toast.makeText(this,"user:"+eml+"  password:"+pwd, 3000).show();

    }

}


/***********************************************/
ps:   布局文件加个按钮,  别忘记在Manifest增加配制
<activity
            android:name="com.example.postvalue.SecondPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="SecondPage" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图