学习安卓笔记之intent
这是效果图,本实验共有三个Activity,直接上代码吧
1 package com.example.wyaslesson.experiment3; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 13 import com.example.wyaslesson.R; 14 15 import org.w3c.dom.Text; 16 17 public class IntentActivity1 extends AppCompatActivity { 18 19 private Button btn_Ac_In_mood; 20 private EditText et_Ac_In_mood; 21 private Button btn_Ac_In_choose; 22 private TextView tv_intent_ask; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_intent1); 28 setTitle("学习Intent"); 29 30 btn_Ac_In_mood = findViewById(R.id.btn_Ac_In_mood); 31 et_Ac_In_mood = findViewById(R.id.et_Ac_In_mood); 32 btn_Ac_In_choose = findViewById(R.id.btn_Ac_In_choose); 33 tv_intent_ask = findViewById(R.id.tv_intent_ask); 34 35 btn_Ac_In_mood.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View view) { 38 String my_mood; 39 Intent it = new Intent(IntentActivity1.this,IntentActivity2.class); 40 41 my_mood = et_Ac_In_mood.getText().toString(); 42 43 //新建Bundle对象,并把数据写入 44 Bundle bd = new Bundle(); 45 bd.putCharSequence("mood",my_mood); 46 47 it.putExtras(bd);//将数据包Bundle绑定到Intent上 48 startActivity(it); 49 50 finish(); 51 } 52 }); 53 54 btn_Ac_In_choose.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View view) { 57 58 Intent it = new Intent(IntentActivity1.this,IntentActivity3.class); 59 startActivityForResult(it,0x123); 60 } 61 }); 62 } 63 64 @Override 65 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 66 super.onActivityResult(requestCode, resultCode, data); 67 if(resultCode == 0x123 && resultCode == 0x123) { 68 tv_intent_ask.setText(" "); 69 Bundle bd = data.getExtras(); 70 int img_id = bd.getInt("img_id"); 71 String tv_receive = bd.getString("hey"); 72 73 ImageView img = findViewById(R.id.iv_icon); 74 TextView tv = findViewById(R.id.tv_intent_receive); 75 76 img.setImageResource(img_id); 77 tv.setText(tv_receive); 78 } 79 } 80 81 }
1 package com.example.wyaslesson.experiment3; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.AdapterView; 10 import android.widget.BaseAdapter; 11 import android.widget.GridView; 12 import android.widget.ImageView; 13 14 import com.example.wyaslesson.R; 15 16 public class IntentActivity3 extends AppCompatActivity { 17 18 private int[] imgs = new int[]{ 19 R.drawable.ice_cream_1,R.drawable.ice_cream_2, 20 R.drawable.ice_cream_3,R.drawable.ice_cream_4, 21 R.drawable.ice_cream_5,R.drawable.ice_cream_6, 22 R.drawable.ice_cream_7,R.drawable.ice_cream_8, 23 }; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_intent3); 29 setTitle("学习Intent"); 30 31 GridView gd = findViewById(R.id.grid_icon); 32 BaseAdapter baseAdapter = new BaseAdapter() { 33 @Override 34 public int getCount() { 35 return imgs.length; 36 } 37 38 //获取当前选项 39 @Override 40 public Object getItem(int i) { 41 return i; 42 } 43 44 //获取当前选项对应的id 45 @Override 46 public long getItemId(int i) { 47 return i; 48 } 49 50 @Override 51 public View getView(int i, View view, ViewGroup viewGroup) { 52 ImageView imageView; 53 if (view == null) { 54 imageView = new ImageView(IntentActivity3.this); 55 imageView.setAdjustViewBounds(true); 56 imageView.setMaxHeight(111); 57 imageView.setMaxWidth(111); 58 imageView.setPadding(5,5,5,5); 59 } 60 else imageView = (ImageView) view; 61 imageView.setImageResource(imgs[i]); 62 return imageView; 63 } 64 }; 65 66 gd.setAdapter(baseAdapter); 67 68 gd.setOnItemClickListener(new AdapterView.OnItemClickListener() { 69 @Override 70 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 71 Intent it = getIntent(); 72 Bundle bd = new Bundle(); 73 bd.putInt("img_id",imgs[i]); 74 bd.putCharSequence("hey","这是你的雪糕,enjoy! ^-^"); 75 it.putExtras(bd); 76 setResult(0x123,it); 77 finish(); 78 } 79 }); 80 } 81 }
1 package com.example.wyaslesson.experiment3; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.widget.TextView; 8 9 import com.example.wyaslesson.R; 10 11 public class IntentActivity2 extends AppCompatActivity { 12 13 private TextView tv_Ac_In_re_mood; 14 private String my_mood_is; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_intent2); 20 setTitle("学习Intent"); 21 22 tv_Ac_In_re_mood = findViewById(R.id.tv_Ac_In_re_mood); 23 24 Intent it = getIntent(); 25 Bundle bd = it.getExtras(); 26 my_mood_is = bd.getCharSequence("mood").toString(); 27 28 tv_Ac_In_re_mood.setText("今天的心情是" + my_mood_is + "^-^"); 29 } 30 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:id="@+id/tv_Ac_In_re_mood" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:textSize="30sp"/> 11 12 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <EditText 8 android:id="@+id/et_Ac_In_mood" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:hint="在文本框中输入你今天的心情吧^-^"/> 12 13 <Button 14 android:id="@+id/btn_Ac_In_mood" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="点击传递今天的心情"/> 18 19 <TextView 20 android:id="@+id/tv_intent_ask" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:layout_marginTop="80dp" 24 android:text="累了吗?请你吃雪糕" 25 android:textSize="20sp" 26 android:textColor="@color/colorAccent"/> 27 28 <Button 29 android:id="@+id/btn_Ac_In_choose" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:text="选择你喜欢的雪糕吧" 33 /> 34 35 <LinearLayout 36 android:layout_width="match_parent" 37 android:layout_height="60dp" 38 android:orientation="horizontal"> 39 40 <TextView 41 android:id="@+id/tv_intent_receive" 42 android:layout_width="wrap_content" 43 android:layout_height="match_parent" 44 android:textSize="20sp" 45 android:hint="先选择雪糕噢"/> 46 47 <ImageView 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:id="@+id/iv_icon" 51 android:paddingLeft="60dp"/> 52 53 </LinearLayout> 54 55 56 57 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <GridView 7 android:id="@+id/grid_icon" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:numColumns="4" 11 android:verticalSpacing="6dp" 12 android:horizontalSpacing="6dp" 13 android:layout_marginTop="10dp"/> 14 15 </RelativeLayout>
21:10:16
2021-03-27