android 点击跳转,Android响应点击事件页面跳转

这是我Android学习的第一天,第一堂课的作业是写两个button,分别实现点击显示hello world 和图片消息。

实现代码如下:

activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2

3 xmlns:app="http://schemas.android.com/apk/res-auto"

4 xmlns:tools="http://schemas.android.com/tools"

5 android:layout_width="match_parent"

6 android:layout_height="match_parent"

7 tools:context="com.example.zhangqiongwen.homework1.MainActivity"

8 tools:layout_editor_absoluteY="81dp">

9

10

11

12

13

button.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2

4 android:layout_width="match_parent"

5 android:layout_height="match_parent">

6

7

8

10 android:layout_width="380dp"

11 android:layout_height="wrap_content"

12 android:layout_alignParentStart="true"

13 android:layout_alignParentTop="true">

14

15

17 android:layout_width="120dp"

18 android:layout_height="40dp"

19 android:layout_alignParentTop="true"

20 android:layout_alignParentLeft="true"

21 android:onClick="click"

22 android:text="button1" />

23

24

26 android:layout_width="120dp"

27 android:layout_height="40dp"

28 android:onClick="click"

29 android:layout_alignParentTop="true"

30 android:layout_alignParentRight="true"

31 android:text="button2" />

32

33

34

35

page1.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2

4 android:layout_height="match_parent">

5

6

8

9

10

12 android:layout_height="wrap_content"

13 android:layout_centerVertical="true"

14 android:layout_centerHorizontal="true"

15 android:text="Hello world!!"

16 android:textColor="#FF79BC"

17 />

18

19

page2.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2

3 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

4 android:layout_height="match_parent">

5

6

7

8

9 android:layout_width="match_parent"

10 android:layout_height="300dp"

11 android:layout_marginTop="40dp"

12 >

13

14

15 android:id="@+id/picture1"

16 android:layout_width="match_parent"

17 android:layout_height="match_parent"

18 android:layout_alignParentStart="true"

19 android:layout_alignParentTop="true"

20 android:src="@drawable/pictur1" />

21

22

23

24

activity.java:

1 packagecom.example.zhangqiongwen.homework1;2

3 importandroid.app.Activity;4 importandroid.content.Intent;5 importandroid.os.Bundle;6 importandroid.support.annotation.Nullable;7 importandroid.view.View;8 importandroid.widget.Button;9

10

11 public class MainActivity extendsActivity{12 @Override13 protected voidonCreate(@Nullable Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 this.setContentView(R.layout.activity_main);16

17 Button btn1 =(Button)findViewById(R.id.button1);18 Button btn2 =(Button)findViewById(R.id.button2);19

20

21 btn1.setOnClickListener(newView.OnClickListener() {22 @Override23 public voidonClick(View view) {24 Intent i = new Intent(MainActivity.this , page1.class);25 startActivity(i);26 }27 });28

29 btn2.setOnClickListener(newView.OnClickListener() {30 @Override31 public voidonClick(View view) {32 Intent b = new Intent(MainActivity.this , page2.class);33 startActivity(b);34 }35 });36

37 }38 }

page1.java

1 importandroid.app.Activity;2 importandroid.content.Intent;3 importandroid.os.Bundle;4 importandroid.support.annotation.Nullable;5 importandroid.view.ContextMenu;6 importandroid.view.View;7 importandroid.widget.Button;8 importandroid.widget.RelativeLayout;9

10 /**

11 * Created by zhang.qiongwen on 2019/8/10.12 */

13

14 public class page1 extendsActivity {15

16 @Override17 protected voidonCreate(@Nullable Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19

20 setContentView(R.layout.page1);21

22

23 Button btn1 =(Button)findViewById(R.id.button1);24 Button btn2 =(Button)findViewById(R.id.button2);25

26 btn1.setOnClickListener(newView.OnClickListener() {27 @Override28 public voidonClick(View view) {29 Intent i = new Intent(page1.this , page1.class);30 startActivity(i);31 }32 });33

34 btn2.setOnClickListener(newView.OnClickListener() {35 @Override36 public voidonClick(View view) {37 Intent b = new Intent(page1.this , page2.class);38 startActivity(b);39 }40 });41

42

43 }44 }

page2.java

1 packagecom.example.zhangqiongwen.homework1;2

3 importandroid.app.Activity;4 importandroid.content.Intent;5 importandroid.os.Bundle;6 importandroid.support.annotation.Nullable;7 importandroid.view.View;8 importandroid.widget.Button;9

10 /**

11 * Created by zhang.qiongwen on 2019/8/10.12 */

13

14 public class page2 extendsActivity {15

16 @Override17 protected voidonCreate(@Nullable Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19

20 setContentView(R.layout.page2);21

22 Button btn1 =(Button)findViewById(R.id.button1);23 Button btn2 =(Button)findViewById(R.id.button2);24

25 btn1.setOnClickListener(newView.OnClickListener() {26 @Override27 public voidonClick(View view) {28 Intent i = new Intent(page2.this , page1.class);29 startActivity(i);30 }31 });32

33 btn2.setOnClickListener(newView.OnClickListener() {34 @Override35 public voidonClick(View view) {36 Intent b = new Intent(page2.this , page2.class);37 startActivity(b);38 }39 });40

41

42 }43 }

其中使用了intent来连接page1、page2、activity三个事件,分别展示page1.xml和page2.xml,其中page1.java和page2.java都需要来绑定button的点击事件,否则在跳转到page1和page2事件之后无法响应另一个button的点击事件,程序直接关闭,关于intent用法可以参考以下链接https://blog.csdn.net/weixin_38199770/article/details/79391259

另外也可以通过直接判断发生点击事件的Button来响应点击事件,代码如下:

MainActivity.java:

1 packagecom.example.zhangqiongwen.homework1;2

3 importandroid.app.Activity;4 importandroid.content.Intent;5 importandroid.os.Bundle;6 importandroid.support.annotation.Nullable;7 importandroid.view.View;8 importandroid.view.ViewStructure;9 importandroid.widget.Button;10

11

12 public class MainActivity extendsActivity{13 @Override14 protected voidonCreate(@Nullable Bundle savedInstanceState) {15 super.onCreate(savedInstanceState);16 this.setContentView(R.layout.activity_main);17

18

19

20 }21

22 public voidclick(View view){23 int i =view.getId();24

25 switch(i){26

27 caseR.id.button1:28 this.setContentView(R.layout.page1);29 break;30

31 caseR.id.button2:32 this.setContentView(R.layout.page2);33 break;34

35 }36 }37

38 }

另外还有同事给我讲了另外一种思路,将图片和Hellow World文字放在同一个布局文件下,然后按钮点击的作用分别是隐藏TextView、显示ImageView和显示TextView、隐藏ImageView,我在网上进行查找发现似乎要使用ConstrainLayout的布局方式,我现在勉强能够使用Relativelayout布局和Linerlayout布局,问题留存,等待解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解一下 Android 开发中点击事件页面跳转的实现方法。 点击事件: 在 Android 开发中,可以通过设置 View 的 OnClickListener 来实现点击事件。具体实现步骤如下: 1. 在 XML 中定义需要响应点击事件的 View,并设置其 id。 ```xml <Button android:id="@+id/btn_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!" /> ``` 2. 在 Java 代码中获取该 View,并设置其 OnClickListener。 ```java Button btnClick = findViewById(R.id.btn_click); btnClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击事件的处理逻辑 } }); ``` 页面跳转: 在 Android 开发中,可以通过 Intent 实现页面跳转。具体实现步骤如下: 1. 在 XML 中定义需要响应点击事件的 View,并设置其 id。 ```xml <Button android:id="@+id/btn_jump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jump to another activity" /> ``` 2. 在 Java 代码中获取该 View,并设置其 OnClickListener。 ```java Button btnJump = findViewById(R.id.btn_jump); btnJump.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建 Intent 对象,指定需要跳转的目标 Activity Intent intent = new Intent(MainActivity.this, AnotherActivity.class); // 启动目标 Activity startActivity(intent); } }); ``` 需要注意的是,需要在 AndroidManifest.xml 文件中注册目标 Activity。 ```xml <activity android:name=".AnotherActivity" /> ``` 以上就是 Android 开发中点击事件页面跳转的实现方法。希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值