android activity进行跳转和传值

在button android 中可以进行页面跳转:在产生新的activity时,需要在mainfast文件中进行注册,如果不注册就会产生在跳转时有app意外停止的错误,在传值的过程中,如果你需要传递过去的页面也要有值传递过来的时候,需要使用

<pre name="code" class="java"><span style="font-size:18px;"> </span><pre name="code" class="java">startActivityForResult(intent,1);

 
如果是简单的注册一个activity,可以这样
 <activity android:name=".app.ShowContent"></activity>


 

1,页面简单的跳转:

<span style="font-size:18px;">  weather_button = (Button) findViewById(R.id.weather_button);

        weather_button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ShowContent.class);
                MainActivity.this.startActivity(intent);
            }
        });</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">如果需要很小的值进行传递,这可以进行简单的传值</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">  Intent intent = new Intent(MainActivity.this, ShowContent.class);
                intent.putExtra("name","benben");
                MainActivity.this.startActivity(intent);
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">可以这样传值,在接受的页面进行接收:</span>
<pre name="code" class="java"><span style="font-size:18px;">protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_content);


        Intent intent = getIntent();
        Log.e("bu", intent.getStringExtra("name"));
    }</span>

这样在第二个页面中就可以接收到值了,这种方式传值仅仅是单向传值,因为你在传值的过程中没有注册码
 
<span style="font-size:18px;">2,intent 进行双向传值 这里的1,是注册码,在卡发中应该定义为一个常量</span>
<pre name="code" class="java"><span style="font-size:18px;"> weather_button = (Button) findViewById(R.id.weather_button);

        weather_button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ShowContent.class);
                intent.putExtra("name","benben");
                startActivityForResult(intent,1);
            }
        });</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;"> @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        String  name =data.getStringExtra("name");
        Log.e("name",name);
    }
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">在接收端可以这样写</span>
<span style="font-size:18px;">button = (Button) findViewById(R.id.context_back);
        final Intent intent = getIntent();
        Log.e("bu", intent.getStringExtra("name"));


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ShowContent.this,MainActivity.class);
                intent1.putExtra("name","benben");
                setResult(1,intent1);
                finish();
            }
        });
</span>
<span style="font-size:18px;">这里就可以接收了,但是注意最后的finish(),方法,表示这个返回的跳转已经完成,才能进行传递</span>
<span style="font-size:18px;">3,如果数据量比较大,那就应该使用Bundle</span>
<span style="font-size:18px;">
</span>
<pre name="code" class="java"><span style="font-size:18px;"> </span><pre name="code" class="java">public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ShowContent.class);
                //intent.putExtra("name","benben");
                Bundle bundle = new Bundle();
                bundle.putString("name", "benben");
                bundle.putInt("age", 123);
                intent.putExtras(bundle);
                startActivityForResult(intent,1);
            }
        });

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//        String  name =data.getStringExtra("name");
//        Log.e("name",name);

        Bundle bundle = data.getExtras();

        String name = bundle.getString("name");
//        data.getBundleExtra("name");
        Log.e("age",name);
    }


 
</pre>在另外一个activity进行接收和传递
<span style="font-size:18px;">
</span>
<span style="font-size:18px;"> </span><pre name="code" class="java">Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_content);


        button = (Button) findViewById(R.id.context_back);
//        final Intent intent = getIntent();
//        Log.e("bu", intent.getStringExtra("name"));
//


        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        int age = bundle.getInt("age");
        String name = bundle.getString("name");
        Log.e("name",age+name);




        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ShowContent.this,MainActivity.class);
                intent1.putExtra("name","benben");
                setResult(1,intent1);
                finish();
            }
        });
    }

 


 
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值