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

界面布局代碼:
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="學號:"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/cdy1"
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="姓名:"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/cdy2"
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="性別:"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb1"
android:text="男"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb2"
android:text="女"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="學院"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/sp1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:prompt="@string/info"
android:entries="@array/xueyuan">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="專業:"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/cdy3"
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="班級:"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/cdy4"
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/bt1"
android:text="使用顯示啟動"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt2"
android:text="隱式"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA代碼:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner= (Spinner) findViewById(R.id.sp1);
final EditText editText1=(EditText)findViewById(R.id.cdy1);
final EditText editText2=(EditText)findViewById(R.id.cdy2);
final EditText editText3=(EditText)findViewById(R.id.cdy3);
final EditText editText4=(EditText)findViewById(R.id.cdy4);
final Spinner spinner1=(Spinner)findViewById(R.id.sp1);
String array[]={"資訊技術學院","外國語學院","機電學院","商學院","藝術設計學院","珠寶學院","新聞傳播學院"};
ArrayAdapter<String>arrayAdapter1=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,array);
spinner.setAdapter(arrayAdapter1);
Button bt1=(Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5 = spinner1.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg);
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb1:
intent.putExtra("data6", "男");
break;
case R.id.rb2:
intent.putExtra("data6", "女");
break;
}
intent.putExtra("data1", string1);
intent.putExtra("data2", string2);
intent.putExtra("data3", string3);
intent.putExtra("data4", string4);
intent.putExtra("data5", string5);
startActivity(intent);
}
});//隱式
Button bt2=(Button)findViewById(R.id.bt2);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent();
//對應AndroidManifest.xml
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5=spinner1.getSelectedItem().toString();
Intent intent1 = new Intent(MainActivity.this,Main2Activity.class);
RadioGroup rg =(RadioGroup) findViewById(R.id.rg);
switch(rg.getCheckedRadioButtonId()) {
case R.id.rb1:
intent1.putExtra("data6", "男");
break;
case R.id.rb2:
intent1.putExtra("data6", "女");
break;
}
intent1.putExtra("data1", string1);
intent1.putExtra("data2", string2);
intent1.putExtra("data3", string3);
intent1.putExtra("data4", string4);
intent1.putExtra("data5", string5);
startActivity(intent1);
intent1.setAction("text.example1.com");
startActivity(intent1);
}
});
}
@Override
protected void onStart(){
super.onStart();
Log.i("main", "onstart");
}
@Override
protected void onRestart(){
super.onRestart();
Log.i("main","Restart");
}
@Override
protected void onResume(){
super.onResume();
Log.i("main","onResume");
}
@Override
protected void onPause(){
super.onPause();
Log.i("main","onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.i("main","onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("main", "onStop");
}
}
2、 點擊界面1中的按鈕后,跳轉至界面2,將界面1中輸入的內容傳遞至界面2的ListView中顯示,效果如圖2所示,(要求:串列文字大小為28sp,圖片使用Vector格式,從上到下依次使用credit_card_black,account_box_black,wc_black,account_balance_black,school_black,assignment_ind_black)

界面布局代碼:
Java代碼:
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
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 Main2Activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView listView = (ListView)findViewById(R.id.lvdata1);
List<Map<String,Object>> lisetdata=new ArrayList<>();
Map<String,Object> map1= new HashMap<>();
Map<String,Object> map2= new HashMap<>();
Map<String,Object> map3= new HashMap<>();
Map<String,Object> map4= new HashMap<>();
Map<String,Object> map5= new HashMap<>();
Map<String,Object> map6= new HashMap<>();
Intent intent = getIntent();
map1.put("txt",intent.getStringExtra("data1"));
map2.put("txt",intent.getStringExtra("data2"));
map3.put("txt",intent.getStringExtra("data3"));
map4.put("txt",intent.getStringExtra("data4"));
map5.put("txt",intent.getStringExtra("data5"));
map6.put("txt",intent.getStringExtra("data6"));
map1.put("img",R.drawable.ic_3d_rotation_24dp);
map2.put("img",R.drawable.ic_account_balance_24dp);
map3.put("img",R.drawable.ic_assignment_late_24dp);
map4.put("img",R.drawable.ic_credit_card_24dp);
map5.put("img",R.drawable.ic_description_24dp);
map6.put("img",R.drawable.ic_extension_24dp);
lisetdata.add(map1);
lisetdata.add(map2);
lisetdata.add(map3);
lisetdata.add(map4);
lisetdata.add(map5);
lisetdata.add(map6);
SimpleAdapter simpleAdapter=new SimpleAdapter(Main2Activity.this,lisetdata,R.layout.listitern,new String[]{"txt","img"},new int[]{R.id.itemtxt,R.id.iteming});
listView.setAdapter(simpleAdapter);
}
}
注冊表代碼:
<?xml version="1.0" encoding="utf-8"?><application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity2">
<intent-filter>
<action android:name="test.example1.com"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/205012.html
標籤:其他
