Activity組件通信
- 前言
- 一、實驗目的及要求
- 二、實驗內容及步驟
- 任務一:根據下述要求實作對應程式,
- 任務二:在第二個活動中進行操作
前言
使用intent在兩個Activity之間實作資料的傳遞,
即 a activity———資料———> b activity,
傳遞的方式有顯示和隱式兩種,
一、實驗目的及要求
(1)掌握顯示啟動和隱式啟動的方式
(2)掌握Activity間的資料通信
二、實驗內容及步驟
任務一:根據下述要求實作對應程式,
完成啟動界面的設計,要求采用合理布局,使界面效果與圖1所示結果保持一致,( “男”單選按鈕默認選中,學院下拉串列框的內容為:資訊技術學院、外國語學院、機電學院、商學院、藝術設計學院、珠寶學院、新聞傳播學院);左列標簽名字體大小為24sp;點擊“使用顯示啟動”按鈕和“使用隱式啟動”按鈕,均能跳轉至界面2,如圖2所示,前者使用顯示啟動方式,后者使用隱式啟動方式,兩種啟動方式均將界面1中的資料傳遞至界面2中,
界面 1布局:

第一個Activity中:
布局檔案代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學號:"
android:textSize="24sp" />
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="24sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RadioGroup
android:id="@+id/rg_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="24sp" />
<RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"></RadioButton>
<RadioButton
android:id="@+id/rb_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"></RadioButton>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學院:"
android:textSize="24sp" />
<Spinner
android:id="@+id/spin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:entries="@array/spinnerdata"
android:spinnerMode="dialog"></Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="專業:"
android:textSize="24sp" />
<EditText
android:id="@+id/et_pf"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班級:"
android:textSize="24sp" />
<EditText
android:id="@+id/et_class"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_show"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="使用顯示啟動" />
<Button
android:id="@+id/btn_hitshow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="使用隱式啟動" />
</LinearLayout>
</LinearLayout>
注意:這里使用傳遞資料給Spinner是通過陣列資源檔案進行設定的,
資源如下:
<resources>
<string name="app_name">test</string>
<string-array name="spinnerdata">
<item>資訊技術學院</item>
<item>外國語學院</item>
<item>機電學院</item>
<item>商學院</item>
<item>藝術設計學院</item>
<item>珠寶學院</item>
<item>新聞傳播學院</item>
</string-array>
</resources>
MainActivity中填寫:
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonShow, buttonHitShow;
private EditText editTextId, editTextName, editTextPf, editTextClass;
private RadioGroup radioGroup;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViewId();
buttonShow.setOnClickListener(this);
buttonHitShow.setOnClickListener(this);
}
private void getViewId() {
buttonShow = findViewById(R.id.btn_show);
buttonHitShow = findViewById(R.id.btn_hitshow);
editTextId = findViewById(R.id.et_id);
editTextName = findViewById(R.id.et_name);
radioGroup = findViewById(R.id.rg_1);
spinner = findViewById(R.id.spin);
editTextPf = findViewById(R.id.et_pf);
editTextClass = findViewById(R.id.et_class);
}
@Override
public void onClick(View v) {
String editTextId_s = editTextId.getText().toString();
String editTextName_s = editTextName.getText().toString();
String editTextPf_s = editTextPf.getText().toString();
String editTextClass_s = editTextClass.getText().toString();
String spinner_s = spinner.getSelectedItem().toString();
switch (v.getId()) {
case R.id.btn_show:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb_man:
intent.putExtra("sexData", "男");
break;
case R.id.rb_woman:
intent.putExtra("sexData", "女");
break;
}
intent.putExtra("editTextId_s", editTextId_s);
intent.putExtra("editTextName_s", editTextName_s);
intent.putExtra("editTextPf_s", editTextPf_s);
intent.putExtra("editTextClass_s", editTextClass_s);
intent.putExtra("data_s", spinner_s);
startActivity(intent);
break;
case R.id.btn_hitshow:
Intent intent1 = new Intent();
intent1.setAction("com.example.mainactivity2");
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb_man:
intent1.putExtra("sexData", "男");
break;
case R.id.rb_woman:
intent1.putExtra("sexData", "女");
break;
}
intent1.putExtra("editTextId_s", editTextId_s);
intent1.putExtra("editTextName_s", editTextName_s);
intent1.putExtra("editTextPf_s", editTextPf_s);
intent1.putExtra("editTextClass_s", editTextClass_s);
intent1.putExtra("data_s", spinner_s);
startActivity(intent1);
break;
}
}
}
任務二:在第二個活動中進行操作
點擊界面1中的按鈕后,跳轉至界面2,將界面1中輸入的內容傳遞至界面2的ListView中顯示,效果如圖2所示,(要求: 串列文字大小為28sp,圖片使用Vector格式,從上到下依次使用credit_card_black,account_box_black,wc_black,account_balance_black,school_black,assignment_ind_black)界面2布局:

第二個Activity中:
布局檔案代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
子項item布局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="28sp" />
</LinearLayout>
MainActivity中代碼:
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView lv_data = findViewById(R.id.lv_1);
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.activity_main2_item, new String[]{"title", "image"},
new int[]{R.id.title, R.id.image});
lv_data.setAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Intent intent = getIntent();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("editTextId_s"));
map.put("image", R.drawable.credit_card_black);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("editTextName_s"));
map.put("image", R.drawable.account_box_black);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("sexData"));
map.put("image", R.drawable.wc_black);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("data_s"));
map.put("image", R.drawable.account_balance_black);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("editTextPf_s"));
map.put("image", R.drawable.school_black);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", intent.getStringExtra("editTextClass_s"));
map.put("image", R.drawable.assignment_ind_black);
list.add(map);
return list;
}
}
注意:在第二個Activity中需要自行指定一個圖片


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202689.html
標籤:其他
上一篇:Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)
