Android Studio初学笔记-Day18-Intent+Bundle

Intent+Bundle

Intent常用於画面的跳转,也就是Activity之间的切换,而在Intent的使用中常伴随着bundle来辅助传资料,今天就讲解一下他们的合作关系。
首先建立一个新的Activity,我命名为SecondActivity,方法如下
https://ithelp.ithome.com.tw/upload/images/20210908/20139136mOn4gIgPka.png
接者来简单设计两个的layout,这里包含了传值内容的EditText和负责跳转Activity的Button。
https://ithelp.ithome.com.tw/upload/images/20210908/201391362h97QadVAs.png
https://ithelp.ithome.com.tw/upload/images/20210908/20139136ynpvXPwhyQ.png
最後来到今天的重头戏,使用Intent和bundle的时候。

程序码

public class MainActivity extends AppCompatActivity {
    private EditText et1,et2,et3;
    private Button bt1;
    private String content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (Button)findViewById(R.id.bt1);
        et1 = (EditText)findViewById(R.id.et1);
        et2 = (EditText)findViewById(R.id.et2);
        et3 = (EditText)findViewById(R.id.et3);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et1.getText().toString().matches("")){ ㄎ
                    Toast.makeText(MainActivity.this,"说点甚麽吧",Toast.LENGTH_SHORT).show();
                }
                else if(et2.getText().toString().matches("")){
                    Toast.makeText(MainActivity.this,"请输入日期~",Toast.LENGTH_SHORT).show();
                }
                else{
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent();
                    //bundle可以根据要传送的资料选择put的型态,括号内分别是(key,value)
                    bundle.putString("content",et1.getText().toString());
                    bundle.putInt("month",Integer.parseInt(et2.getText().toString()));
                    bundle.putInt("date",Integer.parseInt(et3.getText().toString()));
                    //把bundle绑入Intent
                    intent.putExtras(bundle);
                    //设定要跳转的两个Activity
                    intent.setClass(MainActivity.this,SecondActivity2.class);
                    //启动intent,执行画面跳转
                    startActivity(intent);
                }
            }
        });
    }
}
  • 配合着注解应该很好理解,不过通常Intent使用bundle时,通常代表资料不只一个,可以看到puExtras()的部分,其实如果只需要传第一项数据就可以不用bundle单纯的使用intent.putExtra(),这就留给各位在日後摸索复习拉~
    而在接受资料的Activity的程序码则如下
public class SecondActivity2 extends AppCompatActivity {
    private TextView tx1,tx2,tx3;
    private String content,month,date;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second2);

        tx1 = (TextView)findViewById(R.id.tx1);
        tx2 = (TextView)findViewById(R.id.tx2);
        tx3 = (TextView)findViewById(R.id.tx3);
        //取得bundle,慢慢拆解从Intent中找到附加的资料
        Bundle bundle = getIntent().getExtras();
        //拆出bundle的内容,key为content
        content=bundle.getString("content");
        //拆出bundle的内容,key为mounth,并将Int型态转换为String
        month = String.valueOf(bundle.getInt("month"));
        //拆出bundle的内容,key为date,并将Int型态转换为String
        date = String.valueOf(bundle.getInt("date"));
        tx1.setText(content);
        tx2.setText(month);
        tx3.setText(date);
    }
}
  • 在Intent和bundle的使用上都算是容易上手,这样就完成了画面跳转和Activity之间传值。

成果

https://ithelp.ithome.com.tw/upload/images/20210908/20139136rES2nCXuqp.png
https://ithelp.ithome.com.tw/upload/images/20210908/20139136QM2FEYFEVu.png
今天Intent和bundle就讲到这了,谢谢大家~/images/emoticon/emoticon41.gif


<<:  初学者跪着学JavaScript Day18 : 物件:new Map()

>>:  Day19_CSS语法2

day10 轨迹 (雷)没有就等着被电到飞上天

来部落格看图文并茂文章 补觉鸣诗 前面说防火墙 我们会设定 log 记录功能 而一般防火墙内建空间不...

[ Day 29 | Essay ] 作梦也会梦到内心最深刻的恐惧

上礼拜连假前交出了修改後的电商网站作业, 已经修改到第三版了足足花了一个月(不过不是以每天修改 24...

Day2-看看JDK内有些什麽好用的工具!

前言 工作了好一段时间後,直到那次处理了OOM(Out Of Memory)问题,才发现JDK内有很...

台湾游戏公司最新营收 | 第三季游戏公司营运「冻能」 | 蛤! 神逆转! 老大换人了 老二雄风不再

台湾游戏公司最新营收 | 第三季游戏公司营运「冻能」 | 蛤! 神逆转! 老大换人了 老二雄风不再...

WordPress 如何关闭 XML-RPC 服务,避免资安攻击风险

为了提升 WordPress 站台安全性,我安装了防火墙及防毒外挂 Wordfence,此外挂提供了...