学习了Intent与Bundle的使用,进行应用中的交互

 package com.example.intent;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.*;
 import android.view.View.OnClickListener;
 import android.widget.*;

 public class MainActivity extends Activity {

     RadioGroup RG_OS;
     RadioButton r1,r2,r3;
     Button b1,b2;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         RG_OS=(RadioGroup) this.findViewById(R.id.GR_OS);
         r1=(RadioButton) this.findViewById(R.id.radio0);
         r2=(RadioButton) this.findViewById(R.id.radio1);
         r3=(RadioButton) this.findViewById(R.id.radio2);
         b1=(Button)this.findViewById(R.id.button1);
         b1.setOnClickListener(new ButtonClickListener());

     }
     class ButtonClickListener implements OnClickListener{

         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             Intent myintent=new Intent();
             myintent.setClass(MainActivity.this, MainActivity2.class);
             Bundle mybundle=new Bundle();
             if(r1.isChecked())    mybundle.putString("selected", (String)r1.getText());
             else if(r2.isChecked()) mybundle.putString("selected", (String)r2.getText());
             else if(r3.isChecked()) mybundle.putString("selected", (String)r3.getText());
             myintent.putExtras(mybundle);
             MainActivity.this.startActivity(myintent);
             MainActivity.this.finish();

         }
     }

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

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             return true;
         }
         return super.onOptionsItemSelected(item);
     }
 }

界面1

 package com.example.intent;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.*;
 import android.view.View.OnClickListener;
 import android.widget.*;

 public class MainActivity2 extends Activity {

     Button b_back;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main_activity2);
         b_back=(Button) this.findViewById(R.id.button_back);
         TextView textview=(TextView) this.findViewById(R.id.textView1);
         b_back.setOnClickListener(new bc1());
         Intent myintent=this.getIntent();
         Bundle mybundle=myintent.getExtras();
         String selected=mybundle.getString("selected");
         if(selected=="null"){
             textview.setText("No selected any OS");
         }
         else{
             textview.setText(selected+"is selected");
         }
     }
     class bc1 implements OnClickListener{

         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             Intent myintent=new Intent();
             myintent.setClass(MainActivity2.this, MainActivity.class);
             MainActivity2.this.startActivity(myintent);
             MainActivity2.this.finish();

         }

     }

     @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_activity2, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         if (id == R.id.action_settings) {
             return true;
         }
         return super.onOptionsItemSelected(item);
     }
 }

界面2

最新文章

  1. WEB API 中HTTP的get、post、put,delete 请求方式
  2. Android笔记:获取屏幕信息
  3. Winform程序以Icon的形式显示在任务栏右下角
  4. destroy-method="close"的作用
  5. Ueditor上传图片到本地改造到上传图片到云存储
  6. 火狐谷歌浏览器Json查看插件
  7. Linux 开放服务端口
  8. HTML CSS——background的认识(一)
  9. JDK源码学习系列05----LinkedList
  10. Cox回归模型【生存分析】
  11. 设计模式 --> (3)策略模式
  12. [Codeforces 864D]Make a Permutation!
  13. java中遍历map的几种方法介绍
  14. 初始数据结构(python语言)
  15. Codeforces 596D Wilbur and Trees dp (看题解)
  16. Python中Lambda表达式使用
  17. LeetCode--401--二进制手表
  18. .Net文档下载
  19. linux文件系统命令(1)---概述
  20. 算法笔记_111:第五届蓝桥杯软件类省赛真题(Java本科A组)试题解答

热门文章

  1. Ubuntu提示卷boot仅剩0字节的硬盘空间,解决办法
  2. E:“图片视频”的列表页(taxonomy-cat_media.php)
  3. C# 反射研究
  4. vtkQuadric创建二次曲面
  5. window server 2008配置FTP服务器550 Access is denied. 问题解决办法
  6. 防御CSRF的方法有哪些(一) HTTP 头中自定义属性并验证 CSRF跨站域请求伪造攻击
  7. CSS解决未知高度垂直居中
  8. git仓库的初始化
  9. Maven pom.xml 元素配置说明(一)
  10. C++指针参数引用