这是我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 }

 也可以选择将TextView布局和ImageView布局放在同一个位置,然后通过点击事件改变它们的可见度,从而达到效果,代码如下:

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout 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         <Button
11             android:id="@+id/button1"
12             android:layout_width="120dp"
13             android:layout_height="40dp"
14             android:layout_alignParentTop="true"
15             android:layout_alignParentLeft="true"
16             android:onClick="click"
17             android:text="helloworld!!"
18 
19             app:layout_constraintBottom_toBottomOf="parent"
20             app:layout_constraintTop_toTopOf="parent"
21             app:layout_constraintVertical_bias="0.1"
22 
23             app:layout_constraintLeft_toLeftOf="parent"
24             app:layout_constraintRight_toLeftOf="@+id/button2"
25             app:layout_constraintHorizontal_bias="0.1"
26 
27             />
28 
29         <Button
30             android:id="@+id/button2"
31             android:layout_width="120dp"
32             android:layout_height="40dp"
33             android:onClick="click"
34             android:layout_alignParentTop="true"
35             android:layout_alignParentRight="true"
36             android:text="picture"
37 
38             app:layout_constraintBottom_toBottomOf="parent"
39             app:layout_constraintTop_toTopOf="parent"
40             app:layout_constraintVertical_bias="0.1"
41 
42             app:layout_constraintRight_toRightOf="parent"
43             app:layout_constraintLeft_toRightOf="@+id/button1"
44             app:layout_constraintHorizontal_bias="0.1"
45             />
46 
47         <TextView
48             android:layout_width="wrap_content"
49             android:layout_height="wrap_content"
50             android:text="helloworld!!!"
51             android:layout_marginTop="40dp"
52             android:layout_centerHorizontal="true"
53             android:id="@+id/helloworld"
54             
55             app:layout_constraintBottom_toBottomOf="parent"
56             app:layout_constraintTop_toBottomOf="@+id/button2"
57 
58             app:layout_constraintLeft_toLeftOf="parent"
59             app:layout_constraintRight_toRightOf="parent"
60             />
61 
62         <ImageView
63             android:id="@+id/picture"
64             android:layout_width="wrap_content"
65             android:layout_height="wrap_content"
66             android:layout_marginTop="80dp"
67             android:src="@drawable/pictur1"
68 
69             app:layout_constraintBottom_toBottomOf="@+id/helloworld"
70             app:layout_constraintLeft_toLeftOf="@+id/helloworld"
71 
72             app:layout_constraintRight_toRightOf="@+id/helloworld"
73             app:layout_constraintTop_toBottomOf="@+id/helloworld"
74             >
75         </ImageView>
76         
77 
78 </android.support.constraint.ConstraintLayout>

 

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 import android.widget.ImageView;
11 import android.widget.TextView;
12 
13 
14 public class MainActivity extends Activity{
15     @Override
16     protected void onCreate(@Nullable Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         this.setContentView(R.layout.activity_main);
19 
20         Button btn1 = (Button) findViewById(R.id.button1);
21         Button btn2 = (Button) findViewById(R.id.button2);
22 
23         btn1.setOnClickListener(new btn1Linstener());
24         btn2.setOnClickListener(new btn2Linstener());
25         }
26         class btn1Linstener implements View.OnClickListener{
27 
28             public void onClick(View view){
29                 TextView text = (TextView) findViewById(R.id.helloworld);
30                 ImageView image = (ImageView) findViewById(R.id.picture);
31 
32                 text.setVisibility(View.VISIBLE);
33                 image.setVisibility(View.GONE);
34 
35             }
36     }
37         class btn2Linstener implements View.OnClickListener{
38 
39         public void onClick(View view){
40 
41             TextView text = (TextView) findViewById(R.id.helloworld);
42             ImageView image = (ImageView) findViewById(R.id.picture);
43 
44             text.setVisibility(View.GONE);
45             image.setVisibility(View.VISIBLE);
46 
47         }
48         }
49 }

 

版权声明:本文为FrauleinEule原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/FrauleinEule/p/11335555.html