Android 笔记 Intent and Bundle day7

时间:2022-03-16 20:14:29

学习了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