Android studio 点击按钮 (跳转界面)
2023-04-18 15:03:50 时间
Android studio 点击按钮 (跳转界面)
问题描述
首先,我们有两个Java文件和与之绑定的xml文件。此处以HistoryActivity.java,activity_history.xml 和 EventDetail.java,activity_event_detail.xml为例子。我们要实现在HistoryActivity界面中添加一个按钮,并且点击跳转到EventDetail界面。
在这里插入图片描述
为HistoryActivity界面添加按钮 在其对应的activity_history.xml 中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".HistoryActivity">
<Button
android:id="@+id/History"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Historical Event"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</android.support.constraint.ConstraintLayout>
我们通过android:id="@+id/History"语句讲button的id设置为History,在之后设置点击事件时使用。
为History按钮添加点击事件
在HistoryActivity.java中:
package com.example.xff.tm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.widget.Button;
import android.widget.*;
public class HistoryActivity extends AppCompatActivity {
Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
button = (Button)findViewById(R.id.History);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HistoryActivity.this,EventDetail.class);
startActivity(intent);
}
});
}
}
通过之前定义的button的id来找到对应button,为之设置点击监听。当发生点击事件时,通过Intent进行跳转。
在manifests->AndroidManifest.xml中添加activity(这个步骤通常是添加点击事件之后系统自动生成,可以进行检查)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xff.tm">
<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=".HistoryActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EventDetail"></activity>
</application>
</manifest>
EventDetail.java,activity_event_detail.xml
作为被跳转的界面,这两个文件只需要完成自己的功能即可: EventDetail.java:
package com.example.xff.tm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class EventDetail extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
}
}
activity_event_detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".EventDetail">
</android.support.constraint.ConstraintLayout>
本文转自 (2条消息) Android Studio 点击按钮跳转新界面_闷闷闷闷闷小菇的博客-CSDN博客_android studio点击跳转如有侵权,请联系
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击