这是我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布局,问题留存,等待解决。