基本信息
源码名称:实验二深入理解Activity报告
源码大小:0.41M
文件格式:.doc
开发语言:Java
更新时间:2021-06-21
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
在AndroidManifest.xml文件中配置SecondActivity:
配置Intent的Action属性为com.sise.intent.action.JHY_ACTION;
配置Category属性为com.sise.intent.category.JHY_CATEGORY。使用显式Intent启动SecondActivity,并使用Intent从FirstActiv传递数据到SecondActivity。使用Intent传递数据从SecondActivity返回数据到FirstActivity中去。编写代码,运行程序。
Activity_third.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/box_inset_layout_padding" tools:context=".ThirdActivity" tools:deviceIds="wear"> <TextView android:id="@ id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Action为:com.sise.intent.action.JHY_ACTION" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@ id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Gategory属性为:[com.sise.intent.category.JHY_GATEGORY]" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@ id/textView" /> <Button android:id="@ id/button_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="159dp" android:layout_marginEnd="159dp" android:text="返回主页面" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@ id/textView2" /> </androidx.constraintlayout.widget.ConstraintLayout>
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/box_inset_layout_padding" tools:context=".SecondActivity" tools:deviceIds="wear"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@ id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello_world" /> <Button android:id="@ id/button_back" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button back" /> </LinearLayout> </RelativeLayout>
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@ id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动指定Action、指定Gategory对应的Activity" /> <Button android:id="@ id/button_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="传递数据:Hello word" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@ id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果显示:" /> <EditText android:id="@ id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </RelativeLayout>
3.源程序
ThirdActivity.java
package com.example.intenttest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); setTitle("Intent传递数据"); Button button = (Button)findViewById(R.id.button_back); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ThirdActivity.this,"You Click Button 2",Toast.LENGTH_SHORT).show(); finish(); } }); } }
SecondActivity.java
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_second); EditText editText = (EditText)findViewById(R.id.editText1); Button button = (Button)findViewById(R.id.button_back); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String result = editText.getText().toString(); Intent intent = new Intent(); intent.putExtra("result",result); setResult(1001,intent); finish(); } }); } }
MainActivity.java
package com.example.intenttest; 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.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("软件1801贺佳诚1811030107"); Button button1 = (Button) findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "You Click Button 2", Toast.LENGTH_SHORT).show(); Intent intent = new Intent("com.example.activitytest.ACTION_START"); intent.addCategory("com.example.activitytest.MY_CATEGORY"); startActivity(intent); } }); Button button2 = (Button) findViewById(R.id.button_2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "传递数据:Hello word", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent,1000); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); EditText editText = (EditText) findViewById(R.id.editText1); if (requestCode == 1000 && resultCode == 1001) { String result_value = data.getStringExtra("result"); editText.setText(result_value); } } }