两个活动之间的跳转要通过intent来进行,intent跳转分为隐式的和显示的。

首先xml中定义Button,通过按下按钮实现回调,在回调函数中进行相应intent设置。

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:onClick="sendmessage"
/>

回调函数中:

显示调用:

public void sendmessage(View view)
{
//法一:
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);

//法二:
Intent intent=new Intent();
intent.setClassName(this,"com.example.helloworld.SecondActivity"); //第二个参数为包名中相应的activity
this.startActivity(intent);

//法三:
Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,SecondActivity.class);
intent.setComponent(componentName);
startActivity(intent);

//法四:
Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,"com.example.helloworld.SecondActivity");
intent.setComponent(componentName);
startActivity(intent);
}

//隐式调用:
public void sendmessage(View view)
{
Intent intent=new Intent();

intent.setAction("second.activity");
startActivity(intent);
}

AndroidMainifest.xml中:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="second.activity" />
        <category android:name="android.intent.category.DEFAULT" /> //如果这里也写了LAUNCHAR,那么用户可以直接访问第二个活动,也就是手机上会有两个应用。
</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>


实现了活动间的跳转,如果想要在两个活动间发送数据,那么要用到Bundle:

以显示调用的一种跳转方法为例:

现有两个活动:activity1和activity2,实现activity1中的字符串发送到activity2
在activity1中:
    <EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="send"
></Button>

回调函数中:

public void onclick(View view)
{
EditText s=findViewById(R.id.edit);

Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,Main4Activity.class);
intent.setComponent(componentName);

Bundle bundle=new Bundle();
bundle.putString(s.getText().toString());
intent.putExtras(bundle);

startActivity(intent);
}

在activity2中:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
></TextView>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);

TextView s=findViewById(R.id.text);
Bundle bundle=this.getIntent().getExtras();
String str=bundle.getString("text");

s.setText(str);

}


最新文章

  1. Java 输出流中的flush方法
  2. maven配置远程仓库
  3. Git 小技巧
  4. 易语言5.6 精简破解版[Ctoo]
  5. 新手ui设计师必备——切图规范
  6. tomcat重启session不过期的处理
  7. 使用 HTML5 input 类型提升移动端输入体验
  8. linux笔记:linux常用命令-帮助命令
  9. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
  10. lsof作用
  11. c#基础语言编程-编码
  12. Day05_JAVAEE系列:XML
  13. WPF个人助手更新
  14. [PHP] 简单多进程并发
  15. WORLD 快速线
  16. springmvc中messageConverter用法
  17. centos6.5环境disconf管理端安装配置详解
  18. CSS选择器操作大全
  19. Codeforces 493C - Vasya and Basketball
  20. 实体类在set字段时报空指针异常

热门文章

  1. Java 将PDF转为线性PDF
  2. byte数组(byte[])与MultipartFile相互转化
  3. cmake之Visual studio无法显示头文件
  4. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
  5. 【九度OJ】题目1208:10进制 VS 2进制 解题报告
  6. 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)
  7. WebRTC下 的 NAT 穿透技术
  8. java 堆、栈
  9. Simplicial principal component analysis for density functions in Bayes spaces
  10. [数学]高数部分-Part III 中值定理与一元微分学应用