zoukankan      html  css  js  c++  java
  • android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传

    给一个按钮添加onclick事件

            //获取按钮对象
    Button Aiyo = (Button)findViewById(R.id.button1); Aiyo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {//tv.setText("woceshi"); //弹出提示 Toast.makeText(getApplicationContext(), '你好', Toast.LENGTH_SHORT).show(); } });


    打开新的窗口(activity)

    //创建一个窗体对象
    Intent newWindow = new Intent();
    newWindow.setClass(MainActivity.this, NewWindow.class);
    //第一个是当前窗体类,第二个是新窗体类(窗体名称.class)
    startActivity(newWindow);
    //启动新窗体

    当前窗体传值给新窗体

             这是当前窗体所做的事情

    Intent newWindow = new Intent();
    newWindow.setClass(MainActivity.this, NewWindow.class);
    //新开窗口传值
    Bundle bundle = new Bundle();
    bundle.putString("bundleKey", "zongwenlong");
    newWindow.putExtras(bundle);
    //新开窗口传值 end
    //上面的三行赋值的代码其实有点复杂,也可以写成下面的
    //新窗口传值1
    newWindow.putExtra("key","value");
    //新窗口传值1 end
    startActivity(newWindow);

              新窗体所做的事情

              在新窗体的  oncreate 中写

    //获取前一个窗体传来的值
    Bundle bundle = this.getIntent().getExtras();
    Log.e("zllmsg", bundle.getString("bundleKey"));
    //获取前一个窗体传来的值end    

    新窗口关闭,然后将值回传给老窗口

             老窗口所做的事情

    Intent newWindow = new Intent();
    newWindow.setClass(MainActivity.this, NewWindow.class);
    //新开窗口传值
    Bundle bundle = new Bundle();
    bundle.putString("bundleKey", "zongwenlong");
    newWindow.putExtras(bundle);
    //新开窗口传值 end
    //startActivity(newWindow);
    startActivityForResult(newWindow, 1111);//这个1111是一个唯一码,还要用到


             新窗口所做的事情,写一个按钮事件

    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("zllfanhui", "zonglonglongfanhui");
    intent.putExtras(bundle);
    setResult(1111, intent);
    finish();

            老窗口又要做事情了,实现一个接口

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            Log.e("zllmsg",data.getExtras().getString("zllfanhui"));
        }
  • 相关阅读:
    POJ 2251 Dungeon Master(BFS)
    POJ 1321 棋盘问题 (DFS + 回溯)
    POJ 3009 Curling 2.0(DFS + 模拟)
    Codeforces 702D Road to Post Office(模拟 + 公式推导)
    Codeforces 702A Maximum Increase(dp)
    Codeforces 702C Cellular Network(二分)
    Codeforces 702B Powers of Two
    POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)
    POJ 2488 A Knight's Journey (回溯法 | DFS)
    POJ1094 Sorting It All Out (拓扑排序)
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/4630581.html
Copyright © 2011-2022 走看看