Android响应点击事件页面跳转

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

实现代码如下:

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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 <include layout="@layout/button">
11 
12 </include>
13 </RelativeLayout>

button.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7 
 8     <RelativeLayout
 9         android:id="@+id/button_layout"
10         android:layout_width="380dp"
11         android:layout_height="wrap_content"
12         android:layout_alignParentStart="true"
13         android:layout_alignParentTop="true">
14 
15         <Button
16             android:id="@+id/button1"
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         <Button
25             android:id="@+id/button2"
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     </RelativeLayout>
33 
34 
35 </RelativeLayout>

page1.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <include
 7         layout="@layout/button">
 8     </include>
 9 
10     <TextView
11         android:layout_width="wrap_content"
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 </RelativeLayout>

page2.xml:

 
 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <include layout="@layout/button"/>
 7 
 8     <LinearLayout
 9         android:layout_width="match_parent"
10         android:layout_height="300dp"
11         android:layout_marginTop="40dp"
12         >
13 
14     <ImageView
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     </LinearLayout>
23 
24 </RelativeLayout>

activity.java:

 1 package com.example.zhangqiongwen.homework1;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.annotation.Nullable;
 7 import android.view.View;
 8 import android.widget.Button;
 9 
10 
11 public class MainActivity extends Activity{
12     @Override
13     protected void onCreate(@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(new View.OnClickListener() {
22             @Override
23             public void onClick(View view) {
24                 Intent i = new Intent(MainActivity.this , page1.class);
25                 startActivity(i);
26             }
27         });
28 
29         btn2.setOnClickListener(new View.OnClickListener() {
30             @Override
31             public void onClick(View view) {
32                 Intent b = new Intent(MainActivity.this , page2.class);
33                 startActivity(b);
34             }
35         });
36 
37     }
38 }

page1.java

 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.support.annotation.Nullable;
 5 import android.view.ContextMenu;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.RelativeLayout;
 9 
10 /**
11  * Created by zhang.qiongwen on 2019/8/10.
12  */
13 
14 public class page1 extends Activity {
15 
16     @Override
17     protected void onCreate(@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(new View.OnClickListener() {
27             @Override
28             public void onClick(View view) {
29                 Intent i = new Intent(page1.this , page1.class);
30                 startActivity(i);
31             }
32         });
33 
34         btn2.setOnClickListener(new View.OnClickListener() {
35             @Override
36             public void onClick(View view) {
37                 Intent b = new Intent(page1.this , page2.class);
38                 startActivity(b);
39             }
40         });
41 
42 
43     }
44 }

page2.java

 1 package com.example.zhangqiongwen.homework1;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.annotation.Nullable;
 7 import android.view.View;
 8 import android.widget.Button;
 9 
10 /**
11  * Created by zhang.qiongwen on 2019/8/10.
12  */
13 
14 public class page2 extends Activity {
15 
16     @Override
17     protected void onCreate(@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(new View.OnClickListener() {
26             @Override
27             public void onClick(View view) {
28                 Intent i = new Intent(page2.this , page1.class);
29                 startActivity(i);
30             }
31         });
32 
33         btn2.setOnClickListener(new View.OnClickListener() {
34             @Override
35             public void onClick(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 package com.example.zhangqiongwen.homework1;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.support.annotation.Nullable;
 7 import android.view.View;
 8 import android.view.ViewStructure;
 9 import android.widget.Button;
10 
11 
12 public class MainActivity extends Activity{
13     @Override
14     protected void onCreate(@Nullable Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         this.setContentView(R.layout.activity_main);
17 
18 
19 
20     }
21 
22     public void click(View view){
23         int i = view.getId();
24 
25         switch (i){
26 
27             case R.id.button1:
28                 this.setContentView(R.layout.page1);
29                 break;
30 
31             case R.id.button2:
32                 this.setContentView(R.layout.page2);
33                 break;
34 
35         }
36     }
37 
38 }

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

猜你喜欢

转载自www.cnblogs.com/FrauleinEule/p/11335555.html